*DOFILE - MARTIN BAEKGAARD, PAMELA HERD, AND DONALD P. MOYNIHAN: "OF "WELFARE QUEENS" AND "POOR CARINAS": SOCIAL CONSTRUCTIONS, DESERVINGNESS MESSAGING AND THE MENTAL HEALTH OF WELFARE CLIENTS". 
*ACCEPTED FOR PUBLICATION IN BRITISH JOURNAL OF POLITICAL SCIENCE.

***CONTENT DOFIL***
*Preparing the data sets used to analyze the research question (comparing recipients in 2011 with themselves back in time): Lines 15-1625
*Descriptive statistics and interrupted time series analysis: Lines 1626-1889
*Placebo analysis1 + analysis of heterogeneity: Lines 1891-1962
*Preparing the dataset for placebo analysis3 + 4: Lines 1966-2261
*Placebo analysis 3 + 4: Lines 2265-2403
*Preparing the dataset for placebo analysis 2 (including figure S1 exhibiting parallel trends): Lines 2406-3701
*Placebo analysis 2: Lines 3703-3770
*______________________________________________________________________________________________________________
* Preparing data for the year 2011.

use "E:\rawdata\707063\Grunddata\OFFL2017.dta", clear

*Demarcation: All other than social assistance recipients are excluded.

*Identification of date for "Poor Carina"-case - 28. november 2011
gen yearfra = year(pti_vfra)
gen yeartil = year(pti_vtil)
gen monthfra = month(pti_vfra)
gen dayfra = day(pti_vfra)
gen doyfra = doy(pti_vfra)
gen doytil = doy(pti_vtil)

drop if yearfra > 2011
drop if yeartil < 2011

*Date for Fattig Carina.
*sum doyfra if monthfra == 11 & dayfra == 28  & yearfra == 2011
*doyfra = 332.

gen kthj = 1 if pti_tilstand_kode == 2518
replace kthj = 1 if pti_tilstand_kode == 5080
replace kthj = 1 if pti_tilstand_kode == 7050
replace kthj = 1 if pti_tilstand_kode == 7055
drop if kthj == .

*Generating weekly intervals: For each interval, it is marked whether recipients were unemployed or not.

gen datefra = doyfra - 332 + ((yearfra - 2011) * 365) 
gen datetil = doytil - 332 + ((yeartil - 2011) * 365)

gen weekfra = ceil(datefra/7)
gen weektil = ceil(datetil/7)

gen a0 = 1 if weekfra <= -9 & weektil >= -9 
gen a1 = 1 if weekfra <= -8 & weektil >= -8 
gen a2 = 1 if weekfra <= -7 & weektil >= -7 
gen a3 = 1 if weekfra <= -6 & weektil >= -6
gen a4 = 1 if weekfra <= -5 & weektil >= -5
gen a5 = 1 if weekfra <= -4 & weektil >= -4
gen a6 = 1 if weekfra <= -3 & weektil >= -3 
gen a7 = 1 if weekfra <= -2 & weektil >= -2 
gen a8 = 1 if weekfra <= -1 & weektil >= -1 
gen a9 = 1 if weekfra <= 0 & weektil >= 0
gen a10 = 1 if weekfra <= 1 & weektil >= 1 
gen a11 = 1 if weekfra <= 2 & weektil >= 2
gen a12 = 1 if weekfra <= 3 & weektil >= 3 
gen a13 = 1 if weekfra <= 4 & weektil >= 4 


foreach var of varlist a0-a13 {
               replace `var' = 0 if `var' == .
        }

*Creating a variable describing whether pnr-code received benefits in the week in question.
foreach var of varlist a0-a13 {
               bysort pnr: egen a`var' = max(`var')
        }

drop yearfra-a13

*Counting how many intervals individuals received benefits out of total period.
egen sumkthj = rowtotal(aa6-aa13)
collapse (mean) aa0-sumkthj , by(pnr)
drop if sumkthj != 8 
drop sumkthj

destring pnr, replace
sort pnr

save "E:\workdata\707063\Fattig Carina\FCafgraens.dta", replace

clear

*___________________________________________________________________________________________________________


**Linking data to medication usage:

use "E:\rawdata\707063\Grunddata\LMDB2011.dta", clear

gen doyfra = doy(eksd)
gen sample = 1 if doyfra >= 262 & doyfra <= 360
sum sample

drop if sample == .

*Generating weekly intervals: For each interval we measure whether respondents received H05 or H06 medication or not.

gen date = doyfra - 332
gen week = ceil(date/7)

gen f0 = 1 if week == -9
gen f1 = 1 if week == -8
gen f2 = 1 if week == -7
gen f3 = 1 if week == -6
gen f4 = 1 if week == -5
gen f5 = 1 if week == -4
gen f6 = 1 if week == -3
gen f7 = 1 if week == -2
gen f8 = 1 if week == -1
gen f9 = 1 if week == 0 
gen f10 = 1 if week == 1
gen f11 = 1 if week == 2
gen f12 = 1 if week == 3
gen f13 = 1 if week == 4

foreach var of varlist f0-f13 {
               replace `var' = 0 if `var' == .
        }

		
**Creating count-variable summarizing the number of times individuals received all medication (N05 + N06) in a given week.

foreach var of varlist f0-f13 {
               bysort pnr: egen a`var' = total(`var')
        }

*Only N05-medication.		
gen atccode = substr(atc,3,1)
destring atccode, replace

gen bf0 = 1 if week == -9 & atccode == 5
gen bf1 = 1 if week == -8 & atccode == 5
gen bf2 = 1 if week == -7 & atccode == 5
gen bf3 = 1 if week == -6 & atccode == 5
gen bf4 = 1 if week == -5 & atccode == 5
gen bf5 = 1 if week == -4 & atccode == 5
gen bf6 = 1 if week == -3 & atccode == 5
gen bf7 = 1 if week == -2 & atccode == 5
gen bf8 = 1 if week == -1 & atccode == 5
gen bf9 = 1 if week == 0  & atccode == 5
gen bf10 = 1 if week == 1 & atccode == 5
gen bf11 = 1 if week == 2 & atccode == 5
gen bf12 = 1 if week == 3 & atccode == 5
gen bf13 = 1 if week == 4 & atccode == 5


foreach var of varlist bf0-bf13 {
               replace `var' = 0 if `var' == .
        }

**Creating count-variable summarizing the number of times individuals received N05-medication in a given week.
foreach var of varlist bf0-bf13 {
               bysort pnr: egen a`var' = total(`var')
        }

*Only N06-medication.		

gen cf0 = 1 if week == -9 & atccode == 6
gen cf1 = 1 if week == -8 & atccode == 6
gen cf2 = 1 if week == -7 & atccode == 6
gen cf3 = 1 if week == -6 & atccode == 6
gen cf4 = 1 if week == -5 & atccode == 6
gen cf5 = 1 if week == -4 & atccode == 6
gen cf6 = 1 if week == -3 & atccode == 6
gen cf7 = 1 if week == -2 & atccode == 6
gen cf8 = 1 if week == -1 & atccode == 6
gen cf9 = 1 if week == 0  & atccode == 6
gen cf10 = 1 if week == 1 & atccode == 6
gen cf11 = 1 if week == 2 & atccode == 6
gen cf12 = 1 if week == 3 & atccode == 6
gen cf13 = 1 if week == 4 & atccode == 6


foreach var of varlist cf0-cf13 {
               replace `var' = 0 if `var' == .
        }

**Creating count-variable summarizing the number of times individuals received N06-medication in a given week.
foreach var of varlist cf0-cf13 {
               bysort pnr: egen a`var' = total(`var')
			   }
	
collapse (mean) af0-acf13 , by(pnr)

drop atccode- bf13
drop cf0-cf13
destring pnr, replace
sort pnr

*Demarcating sample to recipients of social assistance benefits by merging with sample using pnr-number.
merge 1:1 pnr using "E:\workdata\707063\Fattig Carina\FCafgraens.dta"
drop if _merge == 1
drop _merge

		

	foreach var of varlist af0-acf13 {
               replace `var' = 0 if `var' == .
        }
	
save "E:\workdata\707063\Fattig Carina\data2011.dta", replace
	
*___________________________________________________________________________________________________________

reshape long af abf acf aa, i(pnr) j(week)

rename af NO5NO6
rename abf NO5
rename acf NO6
rename aa kthj

save "E:\workdata\707063\Fattig Carina\data2011.dta", replace

clear

*______________________________________________________________________________________________________________

*Linking to data on medical contacts

use "E:\rawdata\707063\Grunddata\SSSY2011.dta"

destring spec2 pnr honuge, replace
sort pnr

*Excluding observations before 31. oktober 2011
drop if honuge < 1138

*Excluding irrevant codes: ernæringstilskud (94) begravelseshjælp (95) tolkebistand  (96) og fonde (97) + others
drop if spec2 >= 94 & spec2 <= 97
drop ydlant speciale afrper

gen week = honuge - 1138
recode kontakt (-1000/-1 = 0) (2/1000 = 1)
recode spec2 (96 = .)

*Generating variabel for number of medical contacts (direkte + indirect) (as well as one for general practitioners only)
gen Ncontacts = 1
gen Praccontacts = 1 if spec2 == 80

*Collapsing dataset, thereby creating pnr-week observations
collapse (sum) kontakt (sum) Ncontacts (sum) Praccontacts, by(pnr week)
sort pnr week


merge 1:1 pnr week using "E:\workdata\707063\Fattig Carina\data2011.dta"
drop if _merge == 1
drop _merge
recode kontakt Ncontacts  Praccontacts (.= 0)

gen ydelse = 1
rename kthj ydelsemodtaget
gen year = 2011

save "E:\workdata\707063\Fattig Carina\data2011.dta", replace

clear

*Identifying persons in 2011-data that should be found in previous years also.

use "E:\workdata\707063\Fattig Carina\data2011.dta", clear

collapse (mean) week, by(pnr)
drop week
save "E:\workdata\707063\Fattig Carina\pnr2011.dta", replace

clear

*Repeating 2011- procedure for the year 2007.

use "E:\rawdata\707063\Grunddata\OFFL2017.dta"

gen yearfra = year(pti_vfra)
gen yeartil = year(pti_vtil)
gen monthfra = month(pti_vfra)
gen dayfra = day(pti_vfra)
gen doyfra = doy(pti_vfra)
gen doytil = doy(pti_vtil)

drop if yearfra > 2007
drop if yeartil < 2007

gen samplekthj = 1 if doyfra >= 262 & doyfra <= 360 
replace samplekthj = 1 if doytil >= 262 & doytil <= 360 

drop if samplekthj == .

destring pnr, replace
sort pnr pti_vfra

merge m:1 pnr using "E:\workdata\707063\Fattig Carina\pnr2011.dta"

drop if _merge == 1
drop _merge

gen kthj = 1 if pti_tilstand_kode == 2518
replace kthj = 1 if pti_tilstand_kode == 5080
replace kthj = 1 if pti_tilstand_kode == 7050
replace kthj = 1 if pti_tilstand_kode == 7055

gen datefra = doyfra - 332 + ((yearfra - 2007) * 365) 
gen datetil = doytil - 332 + ((yeartil - 2007) * 365)

gen weekfra = ceil(datefra/7)
gen weektil = ceil(datetil/7)

gen a0 = 1 if weekfra <= -9 & weektil >= -9 & kthj == 1
gen a1 = 1 if weekfra <= -8 & weektil >= -8 & kthj == 1
gen a2 = 1 if weekfra <= -7 & weektil >= -7 & kthj == 1
gen a3 = 1 if weekfra <= -6 & weektil >= -6 & kthj == 1
gen a4 = 1 if weekfra <= -5 & weektil >= -5  & kthj == 1
gen a5 = 1 if weekfra <= -4 & weektil >= -4 & kthj == 1
gen a6 = 1 if weekfra <= -3 & weektil >= -3  & kthj == 1
gen a7 = 1 if weekfra <= -2 & weektil >= -2  & kthj == 1
gen a8 = 1 if weekfra <= -1 & weektil >= -1  & kthj == 1
gen a9 = 1 if weekfra <= 0 & weektil >= 0 & kthj == 1
gen a10 = 1 if weekfra <= 1 & weektil >= 1  & kthj == 1
gen a11 = 1 if weekfra <= 2 & weektil >= 2 & kthj == 1
gen a12 = 1 if weekfra <= 3 & weektil >= 3  & kthj == 1
gen a13 = 1 if weekfra <= 4 & weektil >= 4  & kthj == 1

foreach var of varlist a0-a13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist a0-a13 {
               bysort pnr: egen a`var' = max(`var')
        }

drop yearfra-a13

egen sumkthj = rowtotal(aa0-aa13)
collapse (mean) aa0-sumkthj , by(pnr)
drop sumkthj

save "E:\workdata\707063\Fattig Carina\FCafgraens.dta", replace

clear

*___________________________________________________________________________________________________________


use "E:\rawdata\707063\Grunddata\LMDB2007.dta", clear

gen doyfra = doy(eksd)
gen sample = 1 if doyfra >= 272 & doyfra <= 360

drop if sample == .

gen date = doyfra - 332
gen week = ceil(date/7)

gen f0 = 1 if week == -9
gen f1 = 1 if week == -8
gen f2 = 1 if week == -7
gen f3 = 1 if week == -6
gen f4 = 1 if week == -5
gen f5 = 1 if week == -4
gen f6 = 1 if week == -3
gen f7 = 1 if week == -2
gen f8 = 1 if week == -1
gen f9 = 1 if week == 0 
gen f10 = 1 if week == 1
gen f11 = 1 if week == 2
gen f12 = 1 if week == 3
gen f13 = 1 if week == 4

foreach var of varlist f0-f13 {
               replace `var' = 0 if `var' == .
        }

		
foreach var of varlist f0-f13 {
               bysort pnr: egen a`var' = total(`var')
        }

gen atccode = substr(atc,3,1)
destring atccode, replace

gen bf0 = 1 if week == -9 & atccode == 5
gen bf1 = 1 if week == -8 & atccode == 5
gen bf2 = 1 if week == -7 & atccode == 5
gen bf3 = 1 if week == -6 & atccode == 5
gen bf4 = 1 if week == -5 & atccode == 5
gen bf5 = 1 if week == -4 & atccode == 5
gen bf6 = 1 if week == -3 & atccode == 5
gen bf7 = 1 if week == -2 & atccode == 5
gen bf8 = 1 if week == -1 & atccode == 5
gen bf9 = 1 if week == 0  & atccode == 5
gen bf10 = 1 if week == 1 & atccode == 5
gen bf11 = 1 if week == 2 & atccode == 5
gen bf12 = 1 if week == 3 & atccode == 5
gen bf13 = 1 if week == 4 & atccode == 5


foreach var of varlist bf0-bf13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist bf0-bf13 {
               bysort pnr: egen a`var' = total(`var')
        }

gen cf0 = 1 if week == -9 & atccode == 6
gen cf1 = 1 if week == -8 & atccode == 6
gen cf2 = 1 if week == -7 & atccode == 6
gen cf3 = 1 if week == -6 & atccode == 6
gen cf4 = 1 if week == -5 & atccode == 6
gen cf5 = 1 if week == -4 & atccode == 6
gen cf6 = 1 if week == -3 & atccode == 6
gen cf7 = 1 if week == -2 & atccode == 6
gen cf8 = 1 if week == -1 & atccode == 6
gen cf9 = 1 if week == 0  & atccode == 6
gen cf10 = 1 if week == 1 & atccode == 6
gen cf11 = 1 if week == 2 & atccode == 6
gen cf12 = 1 if week == 3 & atccode == 6
gen cf13 = 1 if week == 4 & atccode == 6


foreach var of varlist cf0-cf13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist cf0-cf13 {
               bysort pnr: egen a`var' = total(`var')
			   }
	
collapse (mean) af0-acf13 , by(pnr)

drop atccode- bf13
drop cf0-cf13
destring pnr, replace
sort pnr

merge 1:1 pnr using "E:\workdata\707063\Fattig Carina\FCafgraens.dta"
drop if _merge == 1
drop _merge
		

	foreach var of varlist af0-acf13 {
               replace `var' = 0 if `var' == .
        }
	
save "E:\workdata\707063\Fattig Carina\data2007.dta", replace
	
*___________________________________________________________________________________________________________


reshape long af abf acf aa, i(pnr) j(week)

rename af NO5NO6
rename abf NO5
rename acf NO6
rename aa kthj

save "E:\workdata\707063\Fattig Carina\data2007.dta", replace

clear

*______________________________________________________________________________________________________________


use "E:\rawdata\707063\Grunddata\SSSY2007.dta"

destring spec2 pnr honuge, replace
sort pnr

drop if honuge < 0738

drop if spec2 >= 94 & spec2 <= 97
drop ydlant speciale afrper

gen week = honuge - 0738
recode kontakt (-1000/-1 = 0) (2/1000 = 1)
recode spec2 (96 = .)

gen Ncontacts = 1
gen Praccontacts = 1 if spec2 == 80

collapse (sum) kontakt (sum) Ncontacts (sum) Praccontacts, by(pnr week)
sort pnr week

merge 1:1 pnr week using "E:\workdata\707063\Fattig Carina\data2007.dta"
drop if _merge == 1
drop _merge
recode kontakt Ncontacts  Praccontacts (.= 0)

gen ydelse = 1
rename kthj ydelsemodtaget
gen year = 2007

save "E:\workdata\707063\Fattig Carina\data2007.dta", replace

clear

*_________________________________________________________________________

**Repeating 2011- procedure for the year 2008

use "E:\rawdata\707063\Grunddata\OFFL2017.dta"

gen yearfra = year(pti_vfra)
gen yeartil = year(pti_vtil)
gen monthfra = month(pti_vfra)
gen dayfra = day(pti_vfra)
gen doyfra = doy(pti_vfra)
gen doytil = doy(pti_vtil)

drop if yearfra > 2008
drop if yeartil < 2008

gen samplekthj = 1 if doyfra >= 263 & doyfra <= 361 
replace samplekthj = 1 if doytil >= 263 & doytil <= 361 

drop if samplekthj == .


destring pnr, replace
sort pnr pti_vfra

merge m:1 pnr using "E:\workdata\707063\Fattig Carina\pnr2011.dta"

drop if _merge == 1
drop _merge

gen kthj = 1 if pti_tilstand_kode == 2518
replace kthj = 1 if pti_tilstand_kode == 5080
replace kthj = 1 if pti_tilstand_kode == 7050
replace kthj = 1 if pti_tilstand_kode == 7055


gen datefra = doyfra - 333 + ((yearfra - 2008) * 365) 
gen datetil = doytil - 333 + ((yeartil - 2008) * 365)

gen weekfra = ceil(datefra/7)
gen weektil = ceil(datetil/7)

gen a0 = 1 if weekfra <= -9 & weektil >= -9 
gen a1 = 1 if weekfra <= -8 & weektil >= -8 
gen a2 = 1 if weekfra <= -7 & weektil >= -7 
gen a3 = 1 if weekfra <= -6 & weektil >= -6
gen a4 = 1 if weekfra <= -5 & weektil >= -5
gen a5 = 1 if weekfra <= -4 & weektil >= -4
gen a6 = 1 if weekfra <= -3 & weektil >= -3 
gen a7 = 1 if weekfra <= -2 & weektil >= -2 
gen a8 = 1 if weekfra <= -1 & weektil >= -1 
gen a9 = 1 if weekfra <= 0 & weektil >= 0
gen a10 = 1 if weekfra <= 1 & weektil >= 1 
gen a11 = 1 if weekfra <= 2 & weektil >= 2
gen a12 = 1 if weekfra <= 3 & weektil >= 3 
gen a13 = 1 if weekfra <= 4 & weektil >= 4 


foreach var of varlist a0-a13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist a0-a13 {
               bysort pnr: egen a`var' = max(`var')
        }

drop yearfra-a13

egen sumkthj = rowtotal(aa0-aa13)
collapse (mean) aa0-sumkthj, by(pnr)
drop sumkthj

save "E:\workdata\707063\Fattig Carina\FCafgraens.dta", replace

clear

*___________________________________________________________________________________________________________


use "E:\rawdata\707063\Grunddata\LMDB2008.dta", clear

gen doyfra = doy(eksd)
gen sample = 1 if doyfra >= 273 & doyfra <= 361

drop if sample == .

gen date = doyfra - 333
gen week = ceil(date/7)

gen f0 = 1 if week == -9
gen f1 = 1 if week == -8
gen f2 = 1 if week == -7
gen f3 = 1 if week == -6
gen f4 = 1 if week == -5
gen f5 = 1 if week == -4
gen f6 = 1 if week == -3
gen f7 = 1 if week == -2
gen f8 = 1 if week == -1
gen f9 = 1 if week == 0 
gen f10 = 1 if week == 1
gen f11 = 1 if week == 2
gen f12 = 1 if week == 3
gen f13 = 1 if week == 4

foreach var of varlist f0-f13 {
               replace `var' = 0 if `var' == .
        }

		
foreach var of varlist f0-f13 {
               bysort pnr: egen a`var' = total(`var')
        }

gen atccode = substr(atc,3,1)
destring atccode, replace

gen bf0 = 1 if week == -9 & atccode == 5
gen bf1 = 1 if week == -8 & atccode == 5
gen bf2 = 1 if week == -7 & atccode == 5
gen bf3 = 1 if week == -6 & atccode == 5
gen bf4 = 1 if week == -5 & atccode == 5
gen bf5 = 1 if week == -4 & atccode == 5
gen bf6 = 1 if week == -3 & atccode == 5
gen bf7 = 1 if week == -2 & atccode == 5
gen bf8 = 1 if week == -1 & atccode == 5
gen bf9 = 1 if week == 0  & atccode == 5
gen bf10 = 1 if week == 1 & atccode == 5
gen bf11 = 1 if week == 2 & atccode == 5
gen bf12 = 1 if week == 3 & atccode == 5
gen bf13 = 1 if week == 4 & atccode == 5


foreach var of varlist bf0-bf13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist bf0-bf13 {
               bysort pnr: egen a`var' = total(`var')
        }


gen cf0 = 1 if week == -9 & atccode == 6
gen cf1 = 1 if week == -8 & atccode == 6
gen cf2 = 1 if week == -7 & atccode == 6
gen cf3 = 1 if week == -6 & atccode == 6
gen cf4 = 1 if week == -5 & atccode == 6
gen cf5 = 1 if week == -4 & atccode == 6
gen cf6 = 1 if week == -3 & atccode == 6
gen cf7 = 1 if week == -2 & atccode == 6
gen cf8 = 1 if week == -1 & atccode == 6
gen cf9 = 1 if week == 0  & atccode == 6
gen cf10 = 1 if week == 1 & atccode == 6
gen cf11 = 1 if week == 2 & atccode == 6
gen cf12 = 1 if week == 3 & atccode == 6
gen cf13 = 1 if week == 4 & atccode == 6


foreach var of varlist cf0-cf13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist cf0-cf13 {
               bysort pnr: egen a`var' = total(`var')
			   }
	
collapse (mean) af0-acf13 , by(pnr)

drop atccode- bf13
drop cf0-cf13
destring pnr, replace
sort pnr

merge 1:1 pnr using "E:\workdata\707063\Fattig Carina\FCafgraens.dta"
drop if _merge == 1
drop _merge
		

	foreach var of varlist af0-acf13 {
               replace `var' = 0 if `var' == .
        }
	
save "E:\workdata\707063\Fattig Carina\data2008.dta", replace

	
*___________________________________________________________________________________________________________


reshape long af abf acf aa , i(pnr) j(week)

rename af NO5NO6
rename abf NO5
rename acf NO6
rename aa kthj

save "E:\workdata\707063\Fattig Carina\data2008.dta", replace

clear

*______________________________________________________________________________________________________________


use "E:\rawdata\707063\Grunddata\SSSY2008.dta"

destring spec2 pnr honuge, replace
sort pnr

drop if honuge < 0838

drop if spec2 >= 94 & spec2 <= 97
drop ydlant speciale afrper

gen week = honuge - 0838
recode kontakt (-1000/-1 = 0) (2/1000 = 1)
recode spec2 (96 = .)

gen Ncontacts = 1
gen Praccontacts = 1 if spec2 == 80

collapse (sum) kontakt (sum) Ncontacts (sum) Praccontacts, by(pnr week)
sort pnr week

merge 1:1 pnr week using "E:\workdata\707063\Fattig Carina\data2008.dta"
drop if _merge == 1
drop _merge
recode kontakt Ncontacts  Praccontacts (.= 0)

gen ydelse = 1
rename kthj ydelsemodtaget
gen year = 2008

save "E:\workdata\707063\Fattig Carina\data2008.dta", replace

clear

**Repeating 2011-procedure for the year 2009.

use "E:\rawdata\707063\Grunddata\OFFL2017.dta"

gen yeartil = year(pti_vtil)
gen monthfra = month(pti_vfra)
gen dayfra = day(pti_vfra)
gen doyfra = doy(pti_vfra)
gen doytil = doy(pti_vtil)

drop if yearfra > 2009
drop if yeartil < 2009

gen samplekthj = 1 if doyfra >= 262 & doyfra <= 360 
replace samplekthj = 1 if doytil >= 262 & doytil <= 360 

drop if samplekthj == .


destring pnr, replace
sort pnr pti_vfra

merge m:1 pnr using "E:\workdata\707063\Fattig Carina\pnr2011.dta"

drop if _merge == 1
drop _merge

gen kthj = 1 if pti_tilstand_kode == 2518
replace kthj = 1 if pti_tilstand_kode == 5080
replace kthj = 1 if pti_tilstand_kode == 7050
replace kthj = 1 if pti_tilstand_kode == 7055

gen datefra = doyfra - 332 + ((yearfra - 2009) * 365) 
gen datetil = doytil - 332 + ((yeartil - 2009) * 365)

gen weekfra = ceil(datefra/7)
gen weektil = ceil(datetil/7)


gen a0 = 1 if weekfra <= -9 & weektil >= -9 
gen a1 = 1 if weekfra <= -8 & weektil >= -8 
gen a2 = 1 if weekfra <= -7 & weektil >= -7 
gen a3 = 1 if weekfra <= -6 & weektil >= -6
gen a4 = 1 if weekfra <= -5 & weektil >= -5
gen a5 = 1 if weekfra <= -4 & weektil >= -4
gen a6 = 1 if weekfra <= -3 & weektil >= -3 
gen a7 = 1 if weekfra <= -2 & weektil >= -2 
gen a8 = 1 if weekfra <= -1 & weektil >= -1 
gen a9 = 1 if weekfra <= 0 & weektil >= 0
gen a10 = 1 if weekfra <= 1 & weektil >= 1 
gen a11 = 1 if weekfra <= 2 & weektil >= 2
gen a12 = 1 if weekfra <= 3 & weektil >= 3 
gen a13 = 1 if weekfra <= 4 & weektil >= 4 


foreach var of varlist a0-a13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist a0-a13 {
               bysort pnr: egen a`var' = max(`var')
        }

drop yearfra-a13

egen sumkthj = rowtotal(aa0-aa13)
collapse (mean) aa0-sumkthj , by(pnr)
drop sumkthj

save "E:\workdata\707063\Fattig Carina\FCafgraens.dta", replace

clear

*___________________________________________________________________________________________________________


use "E:\rawdata\707063\Grunddata\LMDB2009.dta", clear

gen doyfra = doy(eksd)
gen sample = 1 if doyfra >= 262 & doyfra <= 360

drop if sample == .

gen date = doyfra - 332
gen week = ceil(date/7)


gen f0 = 1 if week == -9
gen f1 = 1 if week == -8
gen f2 = 1 if week == -7
gen f3 = 1 if week == -6
gen f4 = 1 if week == -5
gen f5 = 1 if week == -4
gen f6 = 1 if week == -3
gen f7 = 1 if week == -2
gen f8 = 1 if week == -1
gen f9 = 1 if week == 0 
gen f10 = 1 if week == 1
gen f11 = 1 if week == 2
gen f12 = 1 if week == 3
gen f13 = 1 if week == 4

foreach var of varlist f0-f13 {
               replace `var' = 0 if `var' == .
        }

	
foreach var of varlist f0-f13 {
               bysort pnr: egen a`var' = total(`var')
        }

gen atccode = substr(atc,3,1)
destring atccode, replace

gen bf0 = 1 if week == -9 & atccode == 5
gen bf1 = 1 if week == -8 & atccode == 5
gen bf2 = 1 if week == -7 & atccode == 5
gen bf3 = 1 if week == -6 & atccode == 5
gen bf4 = 1 if week == -5 & atccode == 5
gen bf5 = 1 if week == -4 & atccode == 5
gen bf6 = 1 if week == -3 & atccode == 5
gen bf7 = 1 if week == -2 & atccode == 5
gen bf8 = 1 if week == -1 & atccode == 5
gen bf9 = 1 if week == 0  & atccode == 5
gen bf10 = 1 if week == 1 & atccode == 5
gen bf11 = 1 if week == 2 & atccode == 5
gen bf12 = 1 if week == 3 & atccode == 5
gen bf13 = 1 if week == 4 & atccode == 5


foreach var of varlist bf0-bf13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist bf0-bf13 {
               bysort pnr: egen a`var' = total(`var')
        }

gen cf0 = 1 if week == -9 & atccode == 6
gen cf1 = 1 if week == -8 & atccode == 6
gen cf2 = 1 if week == -7 & atccode == 6
gen cf3 = 1 if week == -6 & atccode == 6
gen cf4 = 1 if week == -5 & atccode == 6
gen cf5 = 1 if week == -4 & atccode == 6
gen cf6 = 1 if week == -3 & atccode == 6
gen cf7 = 1 if week == -2 & atccode == 6
gen cf8 = 1 if week == -1 & atccode == 6
gen cf9 = 1 if week == 0  & atccode == 6
gen cf10 = 1 if week == 1 & atccode == 6
gen cf11 = 1 if week == 2 & atccode == 6
gen cf12 = 1 if week == 3 & atccode == 6
gen cf13 = 1 if week == 4 & atccode == 6


foreach var of varlist cf0-cf13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist cf0-cf13 {
               bysort pnr: egen a`var' = total(`var')
			   }
	
collapse (mean) af0-acf13 , by(pnr)

drop atccode- bf13
drop cf0-cf13
destring pnr, replace
sort pnr

merge 1:1 pnr using "E:\workdata\707063\Fattig Carina\FCafgraens.dta"
drop if _merge == 1
drop _merge
		

	foreach var of varlist af0-acf13 {
               replace `var' = 0 if `var' == .
        }
	
save "E:\workdata\707063\Fattig Carina\data2009.dta", replace

*___________________________________________________________________________________________________________


reshape long af abf acf aa, i(pnr) j(week)

rename af NO5NO6
rename abf NO5
rename acf NO6
rename aa kthj

save "E:\workdata\707063\Fattig Carina\data2009.dta", replace

clear

*______________________________________________________________________________________________________________


use "E:\rawdata\707063\Grunddata\SSSY2009.dta"

destring spec2 pnr honuge, replace
sort pnr

drop if honuge < 0938

drop if spec2 >= 94 & spec2 <= 97
drop ydlant speciale afrper

gen week = honuge - 0938
recode kontakt (-1000/-1 = 0) (2/1000 = 1)
recode spec2 (96 = .)

gen Ncontacts = 1
gen Praccontacts = 1 if spec2 == 80

collapse (sum) kontakt (sum) Ncontacts (sum) Praccontacts, by(pnr week)
sort pnr week

merge 1:1 pnr week using "E:\workdata\707063\Fattig Carina\data2009.dta"
drop if _merge == 1
drop _merge
recode kontakt Ncontacts  Praccontacts (.= 0)

gen ydelse = 1
rename kthj ydelsemodtaget
gen year = 2009

save "E:\workdata\707063\Fattig Carina\data2009.dta", replace

clear

*_________________________________________________________________________

***Repeating 2011-procedure for the year 2010.


use "E:\rawdata\707063\Grunddata\OFFL2017.dta"

gen yearfra = year(pti_vfra)
gen yeartil = year(pti_vtil)
gen monthfra = month(pti_vfra)
gen dayfra = day(pti_vfra)
gen doyfra = doy(pti_vfra)
gen doytil = doy(pti_vtil)

drop if yearfra > 2010
drop if yeartil < 2010

gen samplekthj = 1 if doyfra >= 262 & doyfra <= 360 
replace samplekthj = 1 if doytil >= 262 & doytil <= 360 

drop if samplekthj == .


destring pnr, replace
sort pnr pti_vfra

merge m:1 pnr using "E:\workdata\707063\Fattig Carina\pnr2011.dta"

drop if _merge == 1
drop _merge

gen kthj = 1 if pti_tilstand_kode == 2518
replace kthj = 1 if pti_tilstand_kode == 5080
replace kthj = 1 if pti_tilstand_kode == 7050
replace kthj = 1 if pti_tilstand_kode == 7055


gen datefra = doyfra - 332 + ((yearfra - 2010) * 365) 
gen datetil = doytil - 332 + ((yeartil - 2010) * 365)

gen weekfra = ceil(datefra/7)
gen weektil = ceil(datetil/7)

gen a0 = 1 if weekfra <= -9 & weektil >= -9 
gen a1 = 1 if weekfra <= -8 & weektil >= -8 
gen a2 = 1 if weekfra <= -7 & weektil >= -7 
gen a3 = 1 if weekfra <= -6 & weektil >= -6
gen a4 = 1 if weekfra <= -5 & weektil >= -5
gen a5 = 1 if weekfra <= -4 & weektil >= -4
gen a6 = 1 if weekfra <= -3 & weektil >= -3 
gen a7 = 1 if weekfra <= -2 & weektil >= -2 
gen a8 = 1 if weekfra <= -1 & weektil >= -1 
gen a9 = 1 if weekfra <= 0 & weektil >= 0
gen a10 = 1 if weekfra <= 1 & weektil >= 1 
gen a11 = 1 if weekfra <= 2 & weektil >= 2
gen a12 = 1 if weekfra <= 3 & weektil >= 3 
gen a13 = 1 if weekfra <= 4 & weektil >= 4 


foreach var of varlist a0-a13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist a0-a13 {
               bysort pnr: egen a`var' = max(`var')
        }

drop yearfra-a13

egen sumkthj = rowtotal(aa0-aa13)
collapse (mean) aa0-sumkthj , by(pnr)
drop sumkthj

save "E:\workdata\707063\Fattig Carina\FCafgraens.dta", replace

clear

*___________________________________________________________________________________________________________

use "E:\rawdata\707063\Grunddata\LMDB2010.dta", clear

gen doyfra = doy(eksd)
gen sample = 1 if doyfra >= 262 & doyfra <= 360

drop if sample == .

gen date = doyfra - 332
gen week = ceil(date/7)

gen f0 = 1 if week == -9
gen f1 = 1 if week == -8
gen f2 = 1 if week == -7
gen f3 = 1 if week == -6
gen f4 = 1 if week == -5
gen f5 = 1 if week == -4
gen f6 = 1 if week == -3
gen f7 = 1 if week == -2
gen f8 = 1 if week == -1
gen f9 = 1 if week == 0 
gen f10 = 1 if week == 1
gen f11 = 1 if week == 2
gen f12 = 1 if week == 3
gen f13 = 1 if week == 4

foreach var of varlist f0-f13 {
               replace `var' = 0 if `var' == .
        }

		
foreach var of varlist f0-f13 {
               bysort pnr: egen a`var' = total(`var')
        }

gen atccode = substr(atc,3,1)
destring atccode, replace

gen bf0 = 1 if week == -9 & atccode == 5
gen bf1 = 1 if week == -8 & atccode == 5
gen bf2 = 1 if week == -7 & atccode == 5
gen bf3 = 1 if week == -6 & atccode == 5
gen bf4 = 1 if week == -5 & atccode == 5
gen bf5 = 1 if week == -4 & atccode == 5
gen bf6 = 1 if week == -3 & atccode == 5
gen bf7 = 1 if week == -2 & atccode == 5
gen bf8 = 1 if week == -1 & atccode == 5
gen bf9 = 1 if week == 0  & atccode == 5
gen bf10 = 1 if week == 1 & atccode == 5
gen bf11 = 1 if week == 2 & atccode == 5
gen bf12 = 1 if week == 3 & atccode == 5
gen bf13 = 1 if week == 4 & atccode == 5


foreach var of varlist bf0-bf13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist bf0-bf13 {
               bysort pnr: egen a`var' = total(`var')
        }

gen cf0 = 1 if week == -9 & atccode == 6
gen cf1 = 1 if week == -8 & atccode == 6
gen cf2 = 1 if week == -7 & atccode == 6
gen cf3 = 1 if week == -6 & atccode == 6
gen cf4 = 1 if week == -5 & atccode == 6
gen cf5 = 1 if week == -4 & atccode == 6
gen cf6 = 1 if week == -3 & atccode == 6
gen cf7 = 1 if week == -2 & atccode == 6
gen cf8 = 1 if week == -1 & atccode == 6
gen cf9 = 1 if week == 0  & atccode == 6
gen cf10 = 1 if week == 1 & atccode == 6
gen cf11 = 1 if week == 2 & atccode == 6
gen cf12 = 1 if week == 3 & atccode == 6
gen cf13 = 1 if week == 4 & atccode == 6


foreach var of varlist cf0-cf13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist cf0-cf13 {
               bysort pnr: egen a`var' = total(`var')
			   }
	
collapse (mean) af0-acf13 , by(pnr)

drop atccode- bf13
drop cf0-cf13
destring pnr, replace
sort pnr

merge 1:1 pnr using "E:\workdata\707063\Fattig Carina\FCafgraens.dta"
drop if _merge == 1
drop _merge
		

	foreach var of varlist af0-acf13 {
               replace `var' = 0 if `var' == .
        }
	
save "E:\workdata\707063\Fattig Carina\data2010.dta", replace

	
*___________________________________________________________________________________________________________


reshape long af abf acf aa, i(pnr) j(week)

rename af NO5NO6
rename abf NO5
rename acf NO6
rename aa kthj

save "E:\workdata\707063\Fattig Carina\data2010.dta", replace

clear

*______________________________________________________________________________________________________________


use "E:\rawdata\707063\Grunddata\SSSY2010.dta"

destring spec2 pnr honuge, replace
sort pnr

drop if honuge < 1037

drop if spec2 >= 94 & spec2 <= 97
drop ydlant speciale afrper

gen week = honuge - 1037
recode kontakt (-1000/-1 = 0) (2/1000 = 1)
recode spec2 (96 = .)

gen Ncontacts = 1
gen Praccontacts = 1 if spec2 == 80

collapse (sum) kontakt (sum) Ncontacts (sum) Praccontacts, by(pnr week)
sort pnr week

merge 1:1 pnr week using "E:\workdata\707063\Fattig Carina\data2010.dta"
drop if _merge == 1
drop _merge
recode kontakt Ncontacts  Praccontacts (.= 0)

gen ydelse = 1
rename kthj ydelsemodtaget
gen year = 2010

save "E:\workdata\707063\Fattig Carina\data2010.dta", replace

clear

*_________________________________________________________________________

*_________________________________________________________________________

use "E:\workdata\707063\Fattig Carina\data2011.dta", clear
append using "E:\workdata\707063\Fattig Carina\data2007.dta"
append using "E:\workdata\707063\Fattig Carina\data2008.dta"
append using "E:\workdata\707063\Fattig Carina\data2009.dta"
append using "E:\workdata\707063\Fattig Carina\data2010.dta"

save "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta", replace

clear

*_____________________________________________________________________________

*Linking to background variables. First: Duration of social assistance benefit recipiency 

use "E:\rawdata\707063\Grunddata\OFFL2017.dta", clear

gen yearfra = year(pti_vfra)
gen yeartil = year(pti_vtil)
gen monthfra = month(pti_vfra)
gen dayfra = day(pti_vfra)
gen doyfra = doy(pti_vfra)
gen doytil = doy(pti_vtil)

drop if yearfra > 2011
drop if yeartil < 2011

sum pti_vfra if yearfra == 2011 & monthfra == 11 & dayfra == 28
gen kontanthjælpstid = 18959 - pti_vfra if pti_vfra <= 18959 & pti_vtil >= 18959
drop if kontanthjælpstid == .

gen kthj = 1 if pti_tilstand_kode == 2518
replace kthj = 1 if pti_tilstand_kode == 5080
replace kthj = 1 if pti_tilstand_kode == 7050
replace kthj = 1 if pti_tilstand_kode == 7055
drop if kthj == .

*Removing doubles that receive multiple benefits simultaneously
collapse (max) kontanthjælpstid, by(pnr)

destring pnr, replace

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta"

drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta", replace

clear

use "E:\rawdata\707063\Grunddata\BEF2011.dta"

bysort familie_id: egen hjemmebarn = max(plads)

gen enligmedbarn2011 = 1 if hjemmebarn == 3 & plads != 3 & familie_type == 5

recode familie_type (1/4=0) (5=1), gen(enlig2011) 

gen birthyear = year(foed_dag)
gen birthmonth = month(foed_dag)
gen birthday = day(foed_dag)

gen alder2011 = 2011 - birthyear
replace alder2011 = 2010 - birthyear if birthmonth == 12
replace alder2011 = 2010 - birthyear if birthmonth == 11 & birthday > 28

recode koen (2=1) (1=0), gen(kvinde)

recode ie_type (1=0) (2=1) (3=0), gen (indvandrer)
recode ie_type (1=0) (2=0) (3=1), gen (efterkommer)

drop birthyear birthmonth birthday
drop aegte_id- hjemmebarn

destring pnr, replace

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta"

drop if _merge == 1
drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta", replace

clear

use "E:\rawdata\707063\Grunddata\BEF2010.dta"

bysort familie_id: egen hjemmebarn = max(plads)

gen enligmedbarn2010 = 1 if hjemmebarn == 3 & plads != 3 & familie_type == 5

recode familie_type (1/4=0) (5=1), gen(enlig2010) 

gen birthyear = year(foed_dag)
gen birthmonth = month(foed_dag)
gen birthday = day(foed_dag)

gen alder2010 = 2010 - birthyear
replace alder2010 = 2009 - birthyear if birthmonth == 12
replace alder2010 = 2009 - birthyear if birthmonth == 11 & birthday > 28

drop birthyear birthmonth birthday
drop aegte_id- hjemmebarn

destring pnr, replace

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta"

drop if _merge == 1
drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta", replace

clear

use "E:\rawdata\707063\Grunddata\BEF2009.dta"

bysort familie_id: egen hjemmebarn = max(plads)

gen enligmedbarn2009 = 1 if hjemmebarn == 3 & plads != 3 & familie_type == 5

recode familie_type (1/4=0) (5=1), gen(enlig2009) 

gen birthyear = year(foed_dag)
gen birthmonth = month(foed_dag)
gen birthday = day(foed_dag)

gen alder2009 = 2009 - birthyear
replace alder2009 = 2008 - birthyear if birthmonth == 12
replace alder2009 = 2008 - birthyear if birthmonth == 11 & birthday > 28

drop birthyear birthmonth birthday
drop aegte_id- hjemmebarn

destring pnr, replace

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta"

drop if _merge == 1
drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta", replace

clear

use "E:\rawdata\707063\Grunddata\BEF2008.dta"

bysort familie_id: egen hjemmebarn = max(plads)

gen enligmedbarn2008 = 1 if hjemmebarn == 3 & plads != 3 & familie_type == 5

recode familie_type (1/4=0) (5=1), gen(enlig2008) 

gen birthyear = year(foed_dag)
gen birthmonth = month(foed_dag)
gen birthday = day(foed_dag)

gen alder2008 = 2008 - birthyear
replace alder2008 = 2007 - birthyear if birthmonth == 12
replace alder2008 = 2007 - birthyear if birthmonth == 11 & birthday > 28

drop birthyear birthmonth birthday
drop aegte_id- hjemmebarn

destring pnr, replace

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta"

drop if _merge == 1
drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta", replace

clear

use "E:\rawdata\707063\Grunddata\BEF2007.dta"

bysort familie_id: egen hjemmebarn = max(plads)

gen enligmedbarn2007 = 1 if hjemmebarn == 3 & plads != 3 & familie_type == 5

recode familie_type (1/4=0) (5=1), gen(enlig2007) 

gen birthyear = year(foed_dag)
gen birthmonth = month(foed_dag)
gen birthday = day(foed_dag)

gen alder2007 = 2007 - birthyear
replace alder2007 = 2006 - birthyear if birthmonth == 12
replace alder2007 = 2006 - birthyear if birthmonth == 11 & birthday > 28

drop birthyear birthmonth birthday
drop aegte_id- hjemmebarn

destring pnr, replace

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta"

drop if _merge == 1
drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta", replace

gen alder = alder2011 if year == 2011
replace alder = alder2010 if year == 2010
replace alder = alder2009 if year == 2009
replace alder = alder2008 if year == 2008
replace alder = alder2007 if year == 2007

gen enligmedbarn = enligmedbarn2011 if year == 2011
replace enligmedbarn = enligmedbarn2010 if year == 2010
replace enligmedbarn = enligmedbarn2009 if year == 2009
replace enligmedbarn = enligmedbarn2008 if year == 2008
replace enligmedbarn = enligmedbarn2007 if year == 2007

gen enlig = enlig2011 if year == 2011
replace enlig = enlig2010 if year == 2010
replace enlig = enlig2009 if year == 2009
replace enlig = enlig2008 if year == 2008
replace enlig = enlig2007 if year == 2007

drop enligmedbarn2007- alder2010
replace enligmedbarn = 0 if enligmedbarn ==. & enlig != .
replace enligmedbarn2011 = 0 if enligmedbarn2011 ==. & enlig2011 != .

save "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta", replace

clear

use "E:\rawdata\707063\Grunddata\UDDF2017.dta"

gen fromyear = year(hf_vfra)
gen toyear = year(hf_vtil)

drop if fromyear > 2011
drop if toyear < 2011

recode hfaudd (1=0) (1006/1023=0) (1106/1123=0) (1208/1523=0) (200=0) (205=0) (210=0) (2508/2511=0)
replace hfaudd = 1 if hfaudd > 0

gen udd2011 = hfaudd 

destring pnr, replace

collapse (max) udd2011, by(pnr)

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta"

drop if _merge == 1
drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta", replace

clear

use "E:\rawdata\707063\Grunddata\UDDF2017.dta"

gen fromyear = year(hf_vfra)
gen toyear = year(hf_vtil)

drop if fromyear > 2010
drop if toyear < 2010

recode hfaudd (1=0) (1006/1023=0) (1106/1123=0) (1208/1523=0) (200=0) (205=0) (210=0) (2508/2511=0)
replace hfaudd = 1 if hfaudd > 0

gen udd2010 = hfaudd 

destring pnr, replace

collapse (max) udd2010, by(pnr)

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta"

drop if _merge == 1
drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta", replace

use "E:\rawdata\707063\Grunddata\UDDF2017.dta"

gen fromyear = year(hf_vfra)
gen toyear = year(hf_vtil)

drop if fromyear > 2009
drop if toyear < 2009

recode hfaudd (1=0) (1006/1023=0) (1106/1123=0) (1208/1523=0) (200=0) (205=0) (210=0) (2508/2511=0)
replace hfaudd = 1 if hfaudd > 0

gen udd2009 = hfaudd 

destring pnr, replace

collapse (max) udd2009, by(pnr)

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta"

drop if _merge == 1
drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta", replace

use "E:\rawdata\707063\Grunddata\UDDF2017.dta"

gen fromyear = year(hf_vfra)
gen toyear = year(hf_vtil)

drop if fromyear > 2008
drop if toyear < 2008

recode hfaudd (1=0) (1006/1023=0) (1106/1123=0) (1208/1523=0) (200=0) (205=0) (210=0) (2508/2511=0)
replace hfaudd = 1 if hfaudd > 0

gen udd2008 = hfaudd 

destring pnr, replace

collapse (max) udd2008, by(pnr)

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta"

drop if _merge == 1
drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta", replace

use "E:\rawdata\707063\Grunddata\UDDF2017.dta"

gen fromyear = year(hf_vfra)
gen toyear = year(hf_vtil)

drop if fromyear > 2007
drop if toyear < 2007

recode hfaudd (1=0) (1006/1023=0) (1106/1123=0) (1208/1523=0) (200=0) (205=0) (210=0) (2508/2511=0)
replace hfaudd = 1 if hfaudd > 0

gen udd2007 = hfaudd 

destring pnr, replace

collapse (max) udd2007, by(pnr)

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta"

drop if _merge == 1
drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta", replace

gen udd = udd2011 if year == 2011
replace udd = udd2010 if year == 2010
replace udd = udd2009 if year == 2009
replace udd = udd2008 if year == 2008
replace udd = udd2007 if year == 2007

drop udd2010 udd2009 udd2008 udd2007

save "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta", replace

clear

**Adding data on whether people received a relevant diagnosis prior to 2011.

*Demarcing the relevant time interval for receiving a diagnosis.

use "E:\rawdata\707063\Grunddata\LPSYADM2016.dta", clear

gen yearfra = year(d_inddto)
drop if yearfra > 2011

*Focusing on diagnoses of the types F32, F33, F41 or F43.
gen str10 diagnosis = substr(c_adiag,2,1)
gen str10 diagnosisa = substr(c_adiag,3,2)
gen diagnosisb = 1 if diagnosis == "F"
gen diagnosisc = 1 if diagnosisa == "32" | diagnosisa == "33" | diagnosisa == "41" | diagnosisa == "43" 

gen diagnosisnum = 1 if diagnosisb == 1 & diagnosisc == 1 

drop if diagnosisnum == .

drop recnum- diagnosisc

save "E:\workdata\707063\Fattig Carina\FCafgraens.dta", replace

clear

use "E:\workdata\707063\Merged data\LPRADM.dta"

gen yearfra = year(d_inddto)
tab yearfra
drop if yearfra > 2011

gen str10 diagnosis = substr(c_adiag,2,1)
gen str10 diagnosisa = substr(c_adiag,3,2)
gen diagnosisb = 1 if diagnosis == "F"
gen diagnosisc = 1 if diagnosisa == "32" | diagnosisa == "33" | diagnosisa == "41" | diagnosisa == "43" 

gen diagnosisnum = 1 if diagnosisb == 1 & diagnosisc == 1 

drop if diagnosisnum == .

drop recnum- diagnosisc

append using "E:\workdata\707063\Fattig Carina\FCafgraens.dta"
collapse (mean) diagnosisnum , by(pnr)

destring pnr, replace

save "E:\workdata\707063\Fattig Carina\FCafgraens.dta", replace

clear

use "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta", clear

merge m:1 pnr using "E:\workdata\707063\Fattig Carina\FCafgraens.dta"

drop if _merge == 2
drop _merge

replace diagnosisnum = 0 if diagnosisnum == .

save "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta", replace


**************Analyses*****************************

cd "E:\workdata\707063\Fattig Carina"

*Analyzing medical prescriptions and health care contacts
use "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta", clear
egen pers = group (pnr year)
egen time = group (year week)
xtset pers week
recode week (0/9 = 0) (10/13 = 1), gen(beforeafterdummy_general)
recode week (0/5 = .) (6/9 = 0) (10/13 = 1), gen(beforeafterdummy)
recode week (0/3 = .) (4/7 = 0) (8/11 = 1) (12/13 = .), gen(beforeafterdummytest1)
recode week (0/1 = .) (2/5 = 0) (6/9 = 1) (10/13 = .), gen(beforeafterdummytest2)
recode week (0/3 = 0) (4/7 = 1) (8/13 = .), gen(beforeafterdummytest3)
replace NO5NO6 = 1 if NO5NO6 > 1
replace NO5 = 1 if NO5 > 1
replace NO6 = 1 if NO6 > 1
replace Ncontacts = 1 if Ncontacts > 1
replace Praccontacts = 1 if Praccontacts > 1
recode year (2007/2010 = 0) (2011 = 1), gen(treatmentyear)

gen enligkvindemedbarn = 1 if enligmedbarn == 1 & kvinde == 1
replace enligkvindemedbarn = 0 if enligmedbarn == 0
replace enligkvindemedbarn = 0 if enligmedbarn == 1 & kvinde == 0

gen enligioevrigt = 1 if enlig ==1 & enligmedbarn == 0
replace  enligioevrigt = 1 if enligmedbarn == 1 & kvinde == 0
replace  enligioevrigt = 0 if enligmedbarn == 1 & kvinde == 1
replace enligioevrigt = 0 if enlig == 0

xtset, clear

xtset pnr time

*Generating descriptive statistics.

drop if week < 6


bysort pnr year: egen maxNO5NO6 = max(NO5NO6)
bysort pnr year: egen maxPraccontacts = max(Praccontacts)

bysort pnr year: egen meanNO5NO6 = mean(NO5NO6)
bysort pnr year: egen meanPraccontacts = mean(Praccontacts)

*Table 1.
sum maxNO5NO6 meanNO5NO6 maxPraccontacts meanPraccontacts kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid if week == 13 & year == 2011

clear

*****

cd "E:\workdata\707063\Fattig Carina"

use "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta", clear
egen pers = group (pnr year)
egen time = group (year week)
xtset pers week
recode week (0/9 = 0) (10/13 = 1), gen(beforeafterdummy_general)
recode week (0/5 = .) (6/9 = 0) (10/13 = 1), gen(beforeafterdummy)
recode week (0/3 = .) (4/7 = 0) (8/11 = 1) (12/13 = .), gen(beforeafterdummytest1)
recode week (0/1 = .) (2/5 = 0) (6/9 = 1) (10/13 = .), gen(beforeafterdummytest2)
recode week (0/3 = 0) (4/7 = 1) (8/13 = .), gen(beforeafterdummytest3)
replace NO5NO6 = 1 if NO5NO6 > 1
replace NO5 = 1 if NO5 > 1
replace NO6 = 1 if NO6 > 1
replace Ncontacts = 1 if Ncontacts > 1
replace Praccontacts = 1 if Praccontacts > 1
recode year (2007/2010 = 0) (2011 = 1), gen(treatmentyear)

gen enligkvindemedbarn = 1 if enligmedbarn == 1 & kvinde == 1
replace enligkvindemedbarn = 0 if enligmedbarn == 0
replace enligkvindemedbarn = 0 if enligmedbarn == 1 & kvinde == 0

gen enligioevrigt = 1 if enlig ==1 & enligmedbarn == 0
replace  enligioevrigt = 1 if enligmedbarn == 1 & kvinde == 0
replace  enligioevrigt = 0 if enligmedbarn == 1 & kvinde == 1
replace enligioevrigt = 0 if enlig == 0

xtset, clear

xtset pnr time

bysort treatmentyear week: sum NO5NO6

*Table 2: Interrupted time series analysis.
eststo: xtlogit NO5NO6 c.beforeafterdummy  if treatmentyear==1, fe
eststo: xtlogit NO5NO6 c.beforeafterdummy kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid if treatmentyear==1, vce(cluster pnr) 
eststo: xtlogit NO5NO6 c.beforeafterdummytest1 c.beforeafterdummy kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid if treatmentyear==1, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummytest2 c.beforeafterdummy kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid if treatmentyear==1, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummytest3 c.beforeafterdummy kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid if treatmentyear==1, vce(cluster pnr)

esttab using discontinuity1.rtf, compress star(* 0.05 ** 0.01 *** 0.001) se ar2
eststo clear

*Table S1: Interrupted time series analysis - supplementary
eststo: xtlogit NO5NO6 c.beforeafterdummy kvinde alder udd indvandrer efterkommer enligioevrigt if treatmentyear==1, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummy kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn if treatmentyear==1, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummy kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid if treatmentyear==1, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummy kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid Praccontacts if treatmentyear==1, vce(cluster pnr)

esttab using discontinuity2.rtf, compress star(* 0.05 ** 0.01 *** 0.001) se ar2
eststo clear

**********************************************************************************************************************************************************
**********************************************************************************************************************************************************

*Table S2.  Interrupted time series analysis - supplementary

*Simpel analyse med diskontinuitetstjek kun i treatmentåret
 
gen dweekk = week - 9

eststo: xtlogit NO5NO6 c.beforeafterdummy#c.dweek i.beforeafterdummy c.dweek if treatmentyear==1, fe

eststo: xtlogit NO5NO6 c.beforeafterdummy#c.dweek i.beforeafterdummy kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid c.dweek if treatmentyear==1, vce(cluster pnr)
predictnl phat1 = predict(pr)

eststo: xtlogit NO5NO6 c.beforeafterdummytest1#c.dweek i.beforeafterdummytest1 kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid c.dweek if treatmentyear==1, vce(cluster pnr)
predictnl phat2 = predict(pr)

eststo: xtlogit NO5NO6 c.beforeafterdummytest2#c.dweek i.beforeafterdummytest2 kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid c.dweek if treatmentyear==1, vce(cluster pnr)
predictnl phat3 = predict(pr)

eststo: xtlogit NO5NO6 c.beforeafterdummytest3#c.dweek i.beforeafterdummytest3 kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid c.dweek if treatmentyear==1, vce(cluster pnr)
predictnl phat4 = predict(pr)

esttab using discontinuity3.rtf, compress star(* 0.05 ** 0.01 *** 0.001) se ar2

bysort beforeafterdummy: sum phat1 if treatmentyear == 1
bysort beforeafterdummytest1: sum phat2 if treatmentyear == 1
bysort beforeafterdummytest2: sum phat3 if treatmentyear == 1
bysort beforeafterdummytest3: sum phat4 if treatmentyear == 1

eststo clear

*Table S3.  Interrupted time series analysis - supplementary

gen dweekanden = dweek * dweek
gen dweektredje = dweek * dweek * dweek

eststo: xtlogit NO5NO6 c.beforeafterdummy#c.dweek i.beforeafterdummy kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid c.dweek c.dweekanden c.beforeafterdummy#c.dweekanden if treatmentyear==1, vce(cluster pnr)
predictnl phat5 = predict(pr)

eststo: xtlogit NO5NO6 c.beforeafterdummytest1#c.dweek i.beforeafterdummytest1 kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid c.dweek c.dweekanden c.beforeafterdummytest1#c.dweekanden if treatmentyear==1, vce(cluster pnr)
predictnl phat6 = predict(pr)

eststo: xtlogit NO5NO6 c.beforeafterdummytest2#c.dweek i.beforeafterdummytest2 kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid c.dweek c.dweekanden c.beforeafterdummytest2#c.dweekanden if treatmentyear==1, vce(cluster pnr)
predictnl phat7 = predict(pr)

eststo: xtlogit NO5NO6 c.beforeafterdummytest3#c.dweek i.beforeafterdummytest3 kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid c.dweek c.dweekanden c.beforeafterdummytest3#c.dweekanden if treatmentyear==1, vce(cluster pnr)
predictnl phat8 = predict(pr)

esttab using discontinuity4.rtf, compress star(* 0.05 ** 0.01 *** 0.001) se ar2

bysort beforeafterdummy: sum phat5 if treatmentyear == 1
bysort beforeafterdummytest1: sum phat6 if treatmentyear == 1
bysort beforeafterdummytest2: sum phat7 if treatmentyear == 1
bysort beforeafterdummytest3: sum phat8 if treatmentyear == 1

eststo clear

eststo: xtlogit NO5NO6 c.beforeafterdummy#c.dweek i.beforeafterdummy kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid c.dweek c.dweekanden c.beforeafterdummy#c.dweekanden c.dweektredje c.beforeafterdummy#c.dweektredje  if treatmentyear==1, vce(cluster pnr)
predictnl phat9 = predict(pr)

eststo: xtlogit NO5NO6 c.beforeafterdummytest1#c.dweek i.beforeafterdummytest1 kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid c.dweek c.dweekanden c.beforeafterdummytest1#c.dweekanden c.dweektredje c.beforeafterdummytest1#c.dweektredje if treatmentyear==1, vce(cluster pnr)
predictnl phat10 = predict(pr)

eststo: xtlogit NO5NO6 c.beforeafterdummytest2#c.dweek i.beforeafterdummytest2 kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid c.dweek c.dweekanden c.beforeafterdummytest2#c.dweekanden c.dweektredje c.beforeafterdummytest2#c.dweektredje if treatmentyear==1, vce(cluster pnr)
predictnl phat11 = predict(pr)

eststo: xtlogit NO5NO6 c.beforeafterdummytest3#c.dweek i.beforeafterdummytest3 kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid c.dweek c.dweekanden c.beforeafterdummytest3#c.dweekanden c.dweektredje c.beforeafterdummytest3#c.dweektredje if treatmentyear==1, vce(cluster pnr)
predictnl phat12 = predict(pr)

esttab using discontinuity5.rtf, compress star(* 0.05 ** 0.01 *** 0.001) se ar2

bysort beforeafterdummy: sum phat9 if treatmentyear == 1
bysort beforeafterdummytest1: sum phat10 if treatmentyear == 1
bysort beforeafterdummytest2: sum phat11 if treatmentyear == 1
bysort beforeafterdummytest3: sum phat12 if treatmentyear == 1

eststo clear

***************************************************************************************************************************************

*Illustrating parallel trends.

eststo: xtlogit NO5NO6 c.beforeafterdummy#c.week i.beforeafterdummy kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid c.week if treatmentyear==1, vce(cluster pnr)
predictnl phat1 = predict(pr), ci(lower1 upper1)

eststo: xtlogit NO5NO6 c.beforeafterdummytest1#c.week i.beforeafterdummytest1 kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid c.week if treatmentyear==1, vce(cluster pnr)
predictnl phat2 = predict(pr), ci(lower2 upper2)

eststo: xtlogit NO5NO6 c.beforeafterdummytest2#c.week i.beforeafterdummytest2 kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid c.week if treatmentyear==1, vce(cluster pnr)
predictnl phat3 = predict(pr), ci(lower3 upper3)

eststo: xtlogit NO5NO6 c.beforeafterdummytest3#c.week i.beforeafterdummytest3 kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid c.week if treatmentyear==1, vce(cluster pnr)
predictnl phat4 = predict(pr), ci(lower4 upper4)

bysort beforeafterdummy: sum phat1 if treatmentyear == 1
bysort beforeafterdummytest1: sum phat2 if treatmentyear == 1
bysort beforeafterdummytest2: sum phat3 if treatmentyear == 1
bysort beforeafterdummytest3: sum phat4 if treatmentyear == 1

esttab using discontinuity2.rtf, compress star(* 0.05 ** 0.01 *** 0.001) se ar2
eststo clear

collapse (mean) phat1 lower1 upper1 phat2 lower2 upper2 phat3 lower3 upper3 phat4 lower4 upper4 NO5NO6 if treatmentyear==1, by(week)
replace week = week - 9
twoway (line phat1 week if week <=0) (line phat1 week if week >0) (line lower1 week if week <=0) (line lower1 week if week >0) (line upper1 week if week <=0) (line upper1 week if week >0) (scatter NO5NO6 week), scheme(s2mono)

save "E:\workdata\707063\Fattig Carina\Parallel trends.dta", clear

*****************************************************************************************************

*Illustrating parallel trends. Continued.

cd "E:\workdata\707063\Fattig Carina"

use "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta", clear
egen pers = group (pnr year)
egen time = group (year week)
xtset pers week
recode week (0/9 = 0) (10/13 = 1), gen(beforeafterdummy_general)
recode week (0/5 = .) (6/9 = 0) (10/13 = 1), gen(beforeafterdummy)
recode week (0/3 = .) (4/7 = 0) (8/11 = 1) (12/13 = .), gen(beforeafterdummytest1)
recode week (0/1 = .) (2/5 = 0) (6/9 = 1) (10/13 = .), gen(beforeafterdummytest2)
recode week (0/3 = 0) (4/7 = 1) (8/13 = .), gen(beforeafterdummytest3)
replace NO5NO6 = 1 if NO5NO6 > 1
replace NO5 = 1 if NO5 > 1
replace NO6 = 1 if NO6 > 1
replace Ncontacts = 1 if Ncontacts > 1
replace Praccontacts = 1 if Praccontacts > 1
recode year (2007/2010 = 0) (2011 = 1), gen(treatmentyear)

gen enligkvindemedbarn = 1 if enligmedbarn == 1 & kvinde == 1
replace enligkvindemedbarn = 0 if enligmedbarn == 0
replace enligkvindemedbarn = 0 if enligmedbarn == 1 & kvinde == 0

gen enligioevrigt = 1 if enlig ==1 & enligmedbarn == 0
replace  enligioevrigt = 1 if enligmedbarn == 1 & kvinde == 0
replace  enligioevrigt = 0 if enligmedbarn == 1 & kvinde == 1
replace enligioevrigt = 0 if enlig == 0

xtset, clear

xtset pnr time

bysort week: egen NO5NO6plac1 = mean(NO5NO6) if treatmentyear==0

collapse (mean) NO5NO6plac1 if treatmentyear == 0, by(week)
replace week = week - 9

merge 1:1 week using "E:\workdata\707063\Fattig Carina\Parallel trends.dta"

drop _merge

twoway (scatter NO5NO6 week) (scatter NO5NO6plac1 week) , scheme(s2mono)

save "E:\workdata\707063\Fattig Carina\Parallel trends.dta", replace

clear

**************************************************************************************************************************************

*Placebo analysis 1

cd "E:\workdata\707063\Fattig Carina"

use "E:\workdata\707063\Fattig Carina\data20072011_same_people.dta", clear
egen pers = group (pnr year)
egen time = group (year week)
xtset pers week
recode week (0/9 = 0) (10/13 = 1), gen(beforeafterdummy_general)
recode week (0/5 = .) (6/9 = 0) (10/13 = 1), gen(beforeafterdummy)
recode week (0/3 = .) (4/7 = 0) (8/11 = 1) (12/13 = .), gen(beforeafterdummytest1)
recode week (0/1 = .) (2/5 = 0) (6/9 = 1) (10/13 = .), gen(beforeafterdummytest2)
recode week (0/3 = 0) (4/7 = 1) (8/13 = .), gen(beforeafterdummytest3)
replace NO5NO6 = 1 if NO5NO6 > 1
replace NO5 = 1 if NO5 > 1
replace NO6 = 1 if NO6 > 1
replace Ncontacts = 1 if Ncontacts > 1
replace Praccontacts = 1 if Praccontacts > 1
recode year (2007/2010 = 0) (2011 = 1), gen(treatmentyear)

gen enligkvindemedbarn = 1 if enligmedbarn == 1 & kvinde == 1
replace enligkvindemedbarn = 0 if enligmedbarn == 0
replace enligkvindemedbarn = 0 if enligmedbarn == 1 & kvinde == 0

gen enligioevrigt = 1 if enlig ==1 & enligmedbarn == 0
replace  enligioevrigt = 1 if enligmedbarn == 1 & kvinde == 0
replace  enligioevrigt = 0 if enligmedbarn == 1 & kvinde == 1
replace enligioevrigt = 0 if enlig == 0

xtset, clear

xtset pnr time

*Table 3 - most-left column)
eststo: xtlogit NO5NO6 c.beforeafterdummy#c.treatmentyear c.treatmentyear kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn, vce(cluster pnr)
margins beforeafterdummy
eststo: xtlogit NO5NO6 c.beforeafterdummy#c.treatmentyear c.treatmentyear kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn i.week, vce(cluster pnr)
margins beforeafterdummy
eststo clear

*Table S4 (supplementary to Table 3)
eststo: xtlogit NO5NO6 c.beforeafterdummy#c.treatmentyear i.week, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummy#c.treatmentyear c.treatmentyear kvinde alder udd indvandrer efterkommer enligioevrigt i.week, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummy#c.treatmentyear c.treatmentyear kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn i.week, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummy#c.treatmentyear c.treatmentyear kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid i.week, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummy#c.treatmentyear c.treatmentyear kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid Praccontacts i.week, vce(cluster pnr)
eststo clear

*Table S7 (supplementary to Table 3)
eststo: xtlogit NO5NO6 c.beforeafterdummy#c.treatmentyear, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummy#c.treatmentyear c.treatmentyear kvinde alder udd indvandrer efterkommer enligioevrigt, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummy#c.treatmentyear c.treatmentyear kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummy#c.treatmentyear c.treatmentyear kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummy#c.treatmentyear c.treatmentyear kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid Praccontacts, vce(cluster pnr)
eststo clear

*********************************************************************************************************************************************************

*Analysis of heterogeneity (Table S11)

eststo: xtlogit NO5NO6 c.beforeafterdummy##c.kontanthjælpstid kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn  if year == 2011, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummy##c.enligkvindemedbarn  kvinde alder udd indvandrer efterkommer enligioevrigt kontanthjælpstid if year == 2011, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummy##c.kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid if year == 2011, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummy##c.Praccontacts kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid if year == 2011, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummy##c.diagnosisnum kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid if year == 2011, vce(cluster pnr)
predictnl pnew = predict(pr)
eststo clear

*Analysis of heterogeneity (Table S12 -left-most column)

eststo: xtlogit NO5NO6 c.beforeafterdummy kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid if year == 2011 & diagnosisnum == 1, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummy kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid if year == 2011 & diagnosisnum == 0, vce(cluster pnr)

***************************************************************************************************************************************************************

*Preparing placebo group 3 and 4 - same procedure as for preparation of placebo 1.

use "E:\rawdata\707063\Grunddata\OFFL2017.dta", clear

gen yearfra = year(pti_vfra)
gen yeartil = year(pti_vtil)
gen monthfra = month(pti_vfra)
gen dayfra = day(pti_vfra)
gen doyfra = doy(pti_vfra)
gen doytil = doy(pti_vtil)

drop if yearfra > 2011
drop if yeartil < 2011

gen kthj = 1 if pti_tilstand_kode == 2518
replace kthj = 1 if pti_tilstand_kode == 5080
replace kthj = 1 if pti_tilstand_kode == 7050
replace kthj = 1 if pti_tilstand_kode == 7055

*Identifying people who received benefits in the time span 1/1 2011 and 30/9 2011 at latest

sum pti_vfra if yearfra == 2011 & monthfra == 1 & dayfra == 1
sum pti_vfra if yearfra == 2011 & monthfra == 9 & dayfra == 30

gen early = 1 if pti_vtil >= 18628 & pti_vtil <= 18900 & kthj == 1
replace early = 0 if early == .

*Identifying people who did not receive social assistance benefits after  30/9 2011 og 5the remainder of the year.

sum pti_vfra if yearfra == 2011 & monthfra == 12 & dayfra == 31

gen late = 1 if pti_vtil >= 18901 & pti_vfra <= 18992 & kthj == 1
replace late = 0 if late == .

gen late2 = 1 if pti_vtil >= 18901 & pti_vfra <= 18992 
replace late2 = 0 if late2 == .

bysort pnr: egen earlynew = max(early)
bysort pnr: egen latenew = max(late)
bysort pnr: egen latenew2 = max(late2)

*Excluding those who did not receive benefitds in the first nine months and those who did receive in the last three months of 2011.
drop if earlynew == 0 
drop if latenew == 1

collapse (mean) latenew2, by(pnr)

destring pnr, replace
sort pnr

save "E:\workdata\707063\Fattig Carina\FCafgraens.dta", replace

clear

*___________________________________________________________________________________________________________


use "E:\rawdata\707063\Grunddata\LMDB2011.dta", clear

gen doyfra = doy(eksd)
gen sample = 1 if doyfra >= 262 & doyfra <= 360
sum sample

drop if sample == .

gen date = doyfra - 332
gen week = ceil(date/7)

gen f0 = 1 if week == -9
gen f1 = 1 if week == -8
gen f2 = 1 if week == -7
gen f3 = 1 if week == -6
gen f4 = 1 if week == -5
gen f5 = 1 if week == -4
gen f6 = 1 if week == -3
gen f7 = 1 if week == -2
gen f8 = 1 if week == -1
gen f9 = 1 if week == 0 
gen f10 = 1 if week == 1
gen f11 = 1 if week == 2
gen f12 = 1 if week == 3
gen f13 = 1 if week == 4

foreach var of varlist f0-f13 {
               replace `var' = 0 if `var' == .
        }

		
foreach var of varlist f0-f13 {
               bysort pnr: egen a`var' = total(`var')
        }

gen atccode = substr(atc,3,1)
destring atccode, replace

gen bf0 = 1 if week == -9 & atccode == 5
gen bf1 = 1 if week == -8 & atccode == 5
gen bf2 = 1 if week == -7 & atccode == 5
gen bf3 = 1 if week == -6 & atccode == 5
gen bf4 = 1 if week == -5 & atccode == 5
gen bf5 = 1 if week == -4 & atccode == 5
gen bf6 = 1 if week == -3 & atccode == 5
gen bf7 = 1 if week == -2 & atccode == 5
gen bf8 = 1 if week == -1 & atccode == 5
gen bf9 = 1 if week == 0  & atccode == 5
gen bf10 = 1 if week == 1 & atccode == 5
gen bf11 = 1 if week == 2 & atccode == 5
gen bf12 = 1 if week == 3 & atccode == 5
gen bf13 = 1 if week == 4 & atccode == 5


foreach var of varlist bf0-bf13 {
               replace `var' = 0 if `var' == .
        }

		
foreach var of varlist bf0-bf13 {
               bysort pnr: egen a`var' = total(`var')
        }

gen cf0 = 1 if week == -9 & atccode == 6
gen cf1 = 1 if week == -8 & atccode == 6
gen cf2 = 1 if week == -7 & atccode == 6
gen cf3 = 1 if week == -6 & atccode == 6
gen cf4 = 1 if week == -5 & atccode == 6
gen cf5 = 1 if week == -4 & atccode == 6
gen cf6 = 1 if week == -3 & atccode == 6
gen cf7 = 1 if week == -2 & atccode == 6
gen cf8 = 1 if week == -1 & atccode == 6
gen cf9 = 1 if week == 0  & atccode == 6
gen cf10 = 1 if week == 1 & atccode == 6
gen cf11 = 1 if week == 2 & atccode == 6
gen cf12 = 1 if week == 3 & atccode == 6
gen cf13 = 1 if week == 4 & atccode == 6

foreach var of varlist cf0-cf13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist cf0-cf13 {
               bysort pnr: egen a`var' = total(`var')
			   }
	
collapse (mean) af0-acf13 , by(pnr)

drop atccode- bf13
drop cf0-cf13
destring pnr, replace
sort pnr

merge m:1 pnr using "E:\workdata\707063\Fattig Carina\FCafgraens.dta"
drop if _merge == 1
drop _merge

	foreach var of varlist af0-acf13 {
               replace `var' = 0 if `var' == .
        }
	
save "E:\workdata\707063\Fattig Carina\dataplacebo.dta", replace

gen aa0 = .
gen aa1 = .
gen aa2 = .
gen aa3 = .
gen aa4 = .
gen aa5 = .
gen aa6 = .
gen aa7 = .
gen aa8 = .
gen aa9 = .
gen aa10 = .
gen aa11 = .
gen aa12 = .
gen aa13 = .

save "E:\workdata\707063\Fattig Carina\dataplacebo.dta", replace
	
*___________________________________________________________________________________________________________

reshape long af abf acf aa, i(pnr) j(week)

rename af NO5NO6
rename abf NO5
rename acf NO6
rename aa kthj

save "E:\workdata\707063\Fattig Carina\dataplacebo.dta", replace

clear

*______________________________________________________________________________________________________________

use "E:\rawdata\707063\Grunddata\SSSY2011.dta"

destring spec2 pnr honuge, replace
sort pnr

drop if honuge < 1138

drop if spec2 >= 94 & spec2 <= 97
drop ydlant speciale afrper

gen week = honuge - 1138
recode kontakt (-1000/-1 = 0) (2/1000 = 1)
recode spec2 (96 = .)

gen Ncontacts = 1
gen Praccontacts = 1 if spec2 == 80

collapse (sum) kontakt (sum) Ncontacts (sum) Praccontacts, by(pnr week)
sort pnr week

merge 1:1 pnr week using "E:\workdata\707063\Fattig Carina\dataplacebo.dta"
drop if _merge == 1
drop _merge
recode kontakt Ncontacts  Praccontacts (.= 0)

gen ydelse = 1
rename kthj ydelsemodtaget
gen year = 4000

save "E:\workdata\707063\Fattig Carina\dataplacebo.dta", replace

clear

use "E:\workdata\707063\Fattig Carina\data2011.dta", clear
append using "E:\workdata\707063\Fattig Carina\dataplacebo.dta"

gen kontanthjælpstid = 0

save "E:\workdata\707063\Fattig Carina\data20072011_placebo.dta", replace

clear

*_____________________________________________________________________________

use "E:\rawdata\707063\Grunddata\BEF2011.dta"

bysort familie_id: egen hjemmebarn = max(plads)

gen enligmedbarn = 1 if hjemmebarn == 3 & plads != 3 & familie_type == 5
replace enligmedbarn = 0 if enligmedbarn == .

recode familie_type (1/4=0) (5=1), gen(enlig) 

gen birthyear = year(foed_dag)
gen birthmonth = month(foed_dag)
gen birthday = day(foed_dag)

gen alder = 2011 - birthyear
replace alder = 2010 - birthyear if birthmonth == 12
replace alder = 2010 - birthyear if birthmonth == 11 & birthday > 28

recode koen (2=1) (1=0), gen(kvinde)

recode ie_type (1=0) (2=1) (3=0), gen (indvandrer)
recode ie_type (1=0) (2=0) (3=1), gen (efterkommer)

drop birthyear birthmonth birthday
drop aegte_id- hjemmebarn

destring pnr, replace

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_placebo.dta"

drop if _merge == 1
drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_placebo.dta", replace

clear

use "E:\rawdata\707063\Grunddata\UDDF2017.dta"

gen fromyear = year(hf_vfra)
gen toyear = year(hf_vtil)

drop if fromyear > 2011
drop if toyear < 2011

recode hfaudd (1=0) (1006/1023=0) (1106/1123=0) (1208/1523=0) (200=0) (205=0) (210=0) (2508/2511=0)
replace hfaudd = 1 if hfaudd > 0

gen udd2011 = hfaudd 

destring pnr, replace

collapse (max) udd2011, by(pnr)

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_placebo.dta"

drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_placebo.dta", replace

clear

***************PLACEBO ANALYSIS 3 + 4****************************

use "E:\workdata\707063\Fattig Carina\data20072011_placebo.dta", clear

cd "E:\workdata\707063\Fattig Carina"

recode week (0/5 = .) (6/9 = 0) (10/13 = 1), gen(beforeafterdummy)
replace NO5NO6 = 1 if NO5NO6 > 1
replace NO5 = 1 if NO5 > 1
replace NO6 = 1 if NO6 > 1
replace Ncontacts = 1 if Ncontacts > 1
replace Praccontacts = 1 if Praccontacts > 1
recode year (4000 = 0) (2011 = 1), gen(treatment)
gen treatment2 = treatment
replace treatment2 = . if latenew2 == 0
sum treatment treatment2

gen enligkvindemedbarn = 1 if enligmedbarn == 1 & kvinde == 1
replace enligkvindemedbarn = 0 if enligmedbarn == 0
replace enligkvindemedbarn = 0 if enligmedbarn == 1 & kvinde == 0

gen enligioevrigt = 1 if enlig ==1 & enligmedbarn == 0
replace  enligioevrigt = 1 if enligmedbarn == 1 & kvinde == 0
replace  enligioevrigt = 0 if enligmedbarn == 1 & kvinde == 1
replace enligioevrigt = 0 if enlig == 0

xtset pnr week

*Calculating parallel trends

bysort week: egen NO5NO6plac3 = mean(NO5NO6) if treatment == 0
bysort week: egen NO5NO6plac4 = mean(NO5NO6) if treatment2 == 0

collapse (mean) NO5NO6plac3 NO5NO6plac4 if year == 4000, by(week)
replace week = week - 9

merge 1:1 week using "E:\workdata\707063\Fattig Carina\Parallel trends.dta"
drop _merge

twoway  (scatter NO5NO6 week) (scatter NO5NO6plac1 week) (scatter NO5NO6plac3 week) (scatter NO5NO6plac4 week), scheme(s2mono)

save "E:\workdata\707063\Fattig Carina\Parallel trends.dta", replace

clear

*Main placebo analysis

use "E:\workdata\707063\Fattig Carina\data20072011_placebo.dta", clear

merge m:1 pnr using "E:\workdata\707063\Fattig Carina\FCafgraens.dta"

drop if _merge == 2
drop _merge

replace diagnosisnum = 0 if diagnosisnum == .

save "E:\workdata\707063\Fattig Carina\data20072011_placebo.dta", replace


cd "E:\workdata\707063\Fattig Carina"

recode week (0/5 = .) (6/9 = 0) (10/13 = 1), gen(beforeafterdummy)
replace NO5NO6 = 1 if NO5NO6 > 1
replace NO5 = 1 if NO5 > 1
replace NO6 = 1 if NO6 > 1
replace Ncontacts = 1 if Ncontacts > 1
replace Praccontacts = 1 if Praccontacts > 1
recode year (4000 = 0) (2011 = 1), gen(treatment)
gen treatment2 = treatment
replace treatment2 = . if latenew2 == 0
sum treatment treatment2

gen enligkvindemedbarn = 1 if enligmedbarn == 1 & kvinde == 1
replace enligkvindemedbarn = 0 if enligmedbarn == 0
replace enligkvindemedbarn = 0 if enligmedbarn == 1 & kvinde == 0

gen enligioevrigt = 1 if enlig ==1 & enligmedbarn == 0
replace  enligioevrigt = 1 if enligmedbarn == 1 & kvinde == 0
replace  enligioevrigt = 0 if enligmedbarn == 1 & kvinde == 1
replace enligioevrigt = 0 if enlig == 0

xtset pnr week

*Analyses reported in Table 3 in paper (for placebo 3 and 4 and in Table S6 and S9 - for full regression results)

eststo: xtlogit NO5NO6 i.beforeafterdummy#c.treatment c.treatment kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn, vce(cluster pnr)
margins beforeafterdummy

eststo: xtlogit NO5NO6 i.beforeafterdummy#c.treatment2 c.treatment2 kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn, vce(cluster pnr)
margins beforeafterdummy

eststo: xtlogit NO5NO6 i.beforeafterdummy#c.treatment c.treatment kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn i.week, vce(cluster pnr)
margins beforeafterdummy

eststo: xtlogit NO5NO6 i.beforeafterdummy#c.treatment2 c.treatment2 kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn i.week, vce(cluster pnr)
margins beforeafterdummy

esttab using placebo3and4.rtf, compress star(* 0.05 ** 0.01 *** 0.001) se ar2
eststo clear

*Table S10

bysort treatment: sum NO5NO6 kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn if week == 13
bysort treatment2: sum NO5NO6 kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn if week == 13

*Reporting heterogeneous treatment effects for Table S12. 

eststo: xtlogit NO5NO6 i.beforeafterdummy#c.treatment c.treatment kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid i.week if diagnosisnum == 0, vce(cluster pnr)


predictnl pnew = predict(pr)
sum pnew if beforeafterdummy == 0 & treatment == 0
sum pnew if beforeafterdummy == 1 & treatment == 0
sum pnew if beforeafterdummy == 0 & treatment == 1
sum pnew if beforeafterdummy == 1 & treatment == 1

eststo: xtlogit NO5NO6 i.beforeafterdummy#c.treatment c.treatment kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid i.week if diagnosisnum == 1, vce(cluster pnr)

predictnl pnew1 = predict(pr)
sum pnew1 if beforeafterdummy == 0 & treatment == 0
sum pnew1 if beforeafterdummy == 1 & treatment == 0
sum pnew1 if beforeafterdummy == 0 & treatment == 1
sum pnew1 if beforeafterdummy == 1 & treatment == 1

eststo: xtlogit NO5NO6 i.beforeafterdummy#c.treatment2 c.treatment2 kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid i.week if diagnosisnum == 0, vce(cluster pnr)

predictnl pnew2 = predict(pr)
sum pnew2 if beforeafterdummy == 0 & treatment2 == 0
sum pnew2 if beforeafterdummy == 1 & treatment2 == 0
sum pnew2 if beforeafterdummy == 0 & treatment2 == 1
sum pnew2 if beforeafterdummy == 1 & treatment2 == 1

eststo: xtlogit NO5NO6 i.beforeafterdummy#c.treatment2 c.treatment2 kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn kontanthjælpstid i.week if diagnosisnum == 1, vce(cluster pnr)

predictnl pnew3 = predict(pr)
sum pnew3 if beforeafterdummy == 0 & treatment2 == 0
sum pnew3 if beforeafterdummy == 1 & treatment2 == 0
sum pnew3 if beforeafterdummy == 0 & treatment2 == 1
sum pnew3 if beforeafterdummy == 1 & treatment2 == 1

esttab using interac3.rtf, compress star(* 0.05 ** 0.01 *** 0.001) se ar2

**************************************************************************************************************************************
***PLACEBOANALYSIS 2- SAME PROCEDURE FOR CONSTRUCTING DATA AS FOR PLACEBO ANALYSIS 1

*2007

use "E:\rawdata\707063\Grunddata\OFFL2017.dta"

gen yearfra = year(pti_vfra)
gen yeartil = year(pti_vtil)
gen monthfra = month(pti_vfra)
gen dayfra = day(pti_vfra)
gen doyfra = doy(pti_vfra)
gen doytil = doy(pti_vtil)

drop if yearfra > 2007
drop if yeartil < 2007

gen kthj = 1 if pti_tilstand_kode == 2518
replace kthj = 1 if pti_tilstand_kode == 5080
replace kthj = 1 if pti_tilstand_kode == 7050
replace kthj = 1 if pti_tilstand_kode == 7055
drop if kthj == .
destring pnr, replace

gen datefra = doyfra - 332 + ((yearfra - 2007) * 365) 
gen datetil = doytil - 332 + ((yeartil - 2007) * 365)

gen weekfra = ceil(datefra/7)
gen weektil = ceil(datetil/7)

gen a0 = 1 if weekfra <= -9 & weektil >= -9 & kthj == 1
gen a1 = 1 if weekfra <= -8 & weektil >= -8 & kthj == 1
gen a2 = 1 if weekfra <= -7 & weektil >= -7 & kthj == 1
gen a3 = 1 if weekfra <= -6 & weektil >= -6 & kthj == 1
gen a4 = 1 if weekfra <= -5 & weektil >= -5  & kthj == 1
gen a5 = 1 if weekfra <= -4 & weektil >= -4 & kthj == 1
gen a6 = 1 if weekfra <= -3 & weektil >= -3  & kthj == 1
gen a7 = 1 if weekfra <= -2 & weektil >= -2  & kthj == 1
gen a8 = 1 if weekfra <= -1 & weektil >= -1  & kthj == 1
gen a9 = 1 if weekfra <= 0 & weektil >= 0 & kthj == 1
gen a10 = 1 if weekfra <= 1 & weektil >= 1  & kthj == 1
gen a11 = 1 if weekfra <= 2 & weektil >= 2 & kthj == 1
gen a12 = 1 if weekfra <= 3 & weektil >= 3  & kthj == 1
gen a13 = 1 if weekfra <= 4 & weektil >= 4  & kthj == 1

foreach var of varlist a0-a13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist a0-a13 {
               bysort pnr: egen a`var' = max(`var')
        }

drop yearfra-a13
egen sumkthj = rowtotal(aa6-aa13)
collapse (mean) aa0-sumkthj , by(pnr)
drop if sumkthj != 8 
drop sumkthj

save "E:\workdata\707063\Fattig Carina\FCafgraens.dta", replace

clear

*___________________________________________________________________________________________________________

use "E:\rawdata\707063\Grunddata\LMDB2007.dta", clear

gen doyfra = doy(eksd)
gen sample = 1 if doyfra >= 272 & doyfra <= 360

drop if sample == .

gen date = doyfra - 332
gen week = ceil(date/7)

gen f0 = 1 if week == -9
gen f1 = 1 if week == -8
gen f2 = 1 if week == -7
gen f3 = 1 if week == -6
gen f4 = 1 if week == -5
gen f5 = 1 if week == -4
gen f6 = 1 if week == -3
gen f7 = 1 if week == -2
gen f8 = 1 if week == -1
gen f9 = 1 if week == 0 
gen f10 = 1 if week == 1
gen f11 = 1 if week == 2
gen f12 = 1 if week == 3
gen f13 = 1 if week == 4

foreach var of varlist f0-f13 {
               replace `var' = 0 if `var' == .
        }

		
foreach var of varlist f0-f13 {
               bysort pnr: egen a`var' = total(`var')
        }

gen atccode = substr(atc,3,1)
destring atccode, replace

gen bf0 = 1 if week == -9 & atccode == 5
gen bf1 = 1 if week == -8 & atccode == 5
gen bf2 = 1 if week == -7 & atccode == 5
gen bf3 = 1 if week == -6 & atccode == 5
gen bf4 = 1 if week == -5 & atccode == 5
gen bf5 = 1 if week == -4 & atccode == 5
gen bf6 = 1 if week == -3 & atccode == 5
gen bf7 = 1 if week == -2 & atccode == 5
gen bf8 = 1 if week == -1 & atccode == 5
gen bf9 = 1 if week == 0  & atccode == 5
gen bf10 = 1 if week == 1 & atccode == 5
gen bf11 = 1 if week == 2 & atccode == 5
gen bf12 = 1 if week == 3 & atccode == 5
gen bf13 = 1 if week == 4 & atccode == 5


foreach var of varlist bf0-bf13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist bf0-bf13 {
               bysort pnr: egen a`var' = total(`var')
        }

gen cf0 = 1 if week == -9 & atccode == 6
gen cf1 = 1 if week == -8 & atccode == 6
gen cf2 = 1 if week == -7 & atccode == 6
gen cf3 = 1 if week == -6 & atccode == 6
gen cf4 = 1 if week == -5 & atccode == 6
gen cf5 = 1 if week == -4 & atccode == 6
gen cf6 = 1 if week == -3 & atccode == 6
gen cf7 = 1 if week == -2 & atccode == 6
gen cf8 = 1 if week == -1 & atccode == 6
gen cf9 = 1 if week == 0  & atccode == 6
gen cf10 = 1 if week == 1 & atccode == 6
gen cf11 = 1 if week == 2 & atccode == 6
gen cf12 = 1 if week == 3 & atccode == 6
gen cf13 = 1 if week == 4 & atccode == 6


foreach var of varlist cf0-cf13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist cf0-cf13 {
               bysort pnr: egen a`var' = total(`var')
			   }
	
collapse (mean) af0-acf13 , by(pnr)

drop atccode- bf13
drop cf0-cf13
destring pnr, replace
sort pnr

merge 1:1 pnr using "E:\workdata\707063\Fattig Carina\FCafgraens.dta"
drop if _merge == 1
drop _merge
		

	foreach var of varlist af0-acf13 {
               replace `var' = 0 if `var' == .
        }
	
save "E:\workdata\707063\Fattig Carina\data2007.dta", replace
	
*___________________________________________________________________________________________________________


reshape long af abf acf aa, i(pnr) j(week)

rename af NO5NO6
rename abf NO5
rename acf NO6
rename aa kthj

save "E:\workdata\707063\Fattig Carina\data2007.dta", replace

clear

*______________________________________________________________________________________________________________


use "E:\rawdata\707063\Grunddata\SSSY2007.dta"

destring spec2 pnr honuge, replace
sort pnr

drop if honuge < 0738

drop if spec2 >= 94 & spec2 <= 97
drop ydlant speciale afrper

gen week = honuge - 0738
recode kontakt (-1000/-1 = 0) (2/1000 = 1)
recode spec2 (96 = .)

gen Ncontacts = 1
gen Praccontacts = 1 if spec2 == 80

collapse (sum) kontakt (sum) Ncontacts (sum) Praccontacts, by(pnr week)
sort pnr week

merge 1:1 pnr week using "E:\workdata\707063\Fattig Carina\data2007.dta"
drop if _merge == 1
drop _merge
recode kontakt Ncontacts  Praccontacts (.= 0)

gen ydelse = 1
rename kthj ydelsemodtaget
gen year = 2007

save "E:\workdata\707063\Fattig Carina\data2007.dta", replace

clear

*_________________________________________________________________________

*2008

use "E:\rawdata\707063\Grunddata\OFFL2017.dta"

gen yearfra = year(pti_vfra)
gen yeartil = year(pti_vtil)
gen monthfra = month(pti_vfra)
gen dayfra = day(pti_vfra)
gen doyfra = doy(pti_vfra)
gen doytil = doy(pti_vtil)

drop if yearfra > 2008
drop if yeartil < 2008

gen kthj = 1 if pti_tilstand_kode == 2518
replace kthj = 1 if pti_tilstand_kode == 5080
replace kthj = 1 if pti_tilstand_kode == 7050
replace kthj = 1 if pti_tilstand_kode == 7055
drop if kthj == .
destring pnr, replace

gen datefra = doyfra - 333 + ((yearfra - 2008) * 365) 
gen datetil = doytil - 333 + ((yeartil - 2008) * 365)

gen weekfra = ceil(datefra/7)
gen weektil = ceil(datetil/7)

gen a0 = 1 if weekfra <= -9 & weektil >= -9 
gen a1 = 1 if weekfra <= -8 & weektil >= -8 
gen a2 = 1 if weekfra <= -7 & weektil >= -7 
gen a3 = 1 if weekfra <= -6 & weektil >= -6
gen a4 = 1 if weekfra <= -5 & weektil >= -5
gen a5 = 1 if weekfra <= -4 & weektil >= -4
gen a6 = 1 if weekfra <= -3 & weektil >= -3 
gen a7 = 1 if weekfra <= -2 & weektil >= -2 
gen a8 = 1 if weekfra <= -1 & weektil >= -1 
gen a9 = 1 if weekfra <= 0 & weektil >= 0
gen a10 = 1 if weekfra <= 1 & weektil >= 1 
gen a11 = 1 if weekfra <= 2 & weektil >= 2
gen a12 = 1 if weekfra <= 3 & weektil >= 3 
gen a13 = 1 if weekfra <= 4 & weektil >= 4 


foreach var of varlist a0-a13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist a0-a13 {
               bysort pnr: egen a`var' = max(`var')
        }

drop yearfra-a13

egen sumkthj = rowtotal(aa6-aa13)
collapse (mean) aa0-sumkthj, by(pnr)
drop if sumkthj != 8 
drop sumkthj

save "E:\workdata\707063\Fattig Carina\FCafgraens.dta", replace

clear

*___________________________________________________________________________________________________________

use "E:\rawdata\707063\Grunddata\LMDB2008.dta", clear

gen doyfra = doy(eksd)
gen sample = 1 if doyfra >= 273 & doyfra <= 361

drop if sample == .

gen date = doyfra - 333
gen week = ceil(date/7)

gen f0 = 1 if week == -9
gen f1 = 1 if week == -8
gen f2 = 1 if week == -7
gen f3 = 1 if week == -6
gen f4 = 1 if week == -5
gen f5 = 1 if week == -4
gen f6 = 1 if week == -3
gen f7 = 1 if week == -2
gen f8 = 1 if week == -1
gen f9 = 1 if week == 0 
gen f10 = 1 if week == 1
gen f11 = 1 if week == 2
gen f12 = 1 if week == 3
gen f13 = 1 if week == 4

foreach var of varlist f0-f13 {
               replace `var' = 0 if `var' == .
        }

		
foreach var of varlist f0-f13 {
               bysort pnr: egen a`var' = total(`var')
        }

gen atccode = substr(atc,3,1)
destring atccode, replace

gen bf0 = 1 if week == -9 & atccode == 5
gen bf1 = 1 if week == -8 & atccode == 5
gen bf2 = 1 if week == -7 & atccode == 5
gen bf3 = 1 if week == -6 & atccode == 5
gen bf4 = 1 if week == -5 & atccode == 5
gen bf5 = 1 if week == -4 & atccode == 5
gen bf6 = 1 if week == -3 & atccode == 5
gen bf7 = 1 if week == -2 & atccode == 5
gen bf8 = 1 if week == -1 & atccode == 5
gen bf9 = 1 if week == 0  & atccode == 5
gen bf10 = 1 if week == 1 & atccode == 5
gen bf11 = 1 if week == 2 & atccode == 5
gen bf12 = 1 if week == 3 & atccode == 5
gen bf13 = 1 if week == 4 & atccode == 5


foreach var of varlist bf0-bf13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist bf0-bf13 {
               bysort pnr: egen a`var' = total(`var')
        }

gen cf0 = 1 if week == -9 & atccode == 6
gen cf1 = 1 if week == -8 & atccode == 6
gen cf2 = 1 if week == -7 & atccode == 6
gen cf3 = 1 if week == -6 & atccode == 6
gen cf4 = 1 if week == -5 & atccode == 6
gen cf5 = 1 if week == -4 & atccode == 6
gen cf6 = 1 if week == -3 & atccode == 6
gen cf7 = 1 if week == -2 & atccode == 6
gen cf8 = 1 if week == -1 & atccode == 6
gen cf9 = 1 if week == 0  & atccode == 6
gen cf10 = 1 if week == 1 & atccode == 6
gen cf11 = 1 if week == 2 & atccode == 6
gen cf12 = 1 if week == 3 & atccode == 6
gen cf13 = 1 if week == 4 & atccode == 6


foreach var of varlist cf0-cf13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist cf0-cf13 {
               bysort pnr: egen a`var' = total(`var')
			   }
	
collapse (mean) af0-acf13 , by(pnr)

drop atccode- bf13
drop cf0-cf13
destring pnr, replace
sort pnr

merge 1:1 pnr using "E:\workdata\707063\Fattig Carina\FCafgraens.dta"
drop if _merge == 1
drop _merge
		

	foreach var of varlist af0-acf13 {
               replace `var' = 0 if `var' == .
        }
	
save "E:\workdata\707063\Fattig Carina\data2008.dta", replace

	
*___________________________________________________________________________________________________________


reshape long af abf acf aa , i(pnr) j(week)

rename af NO5NO6
rename abf NO5
rename acf NO6
rename aa kthj

save "E:\workdata\707063\Fattig Carina\data2008.dta", replace

clear

*______________________________________________________________________________________________________________

use "E:\rawdata\707063\Grunddata\SSSY2008.dta"

destring spec2 pnr honuge, replace
sort pnr

drop if honuge < 0838

drop if spec2 >= 94 & spec2 <= 97
drop ydlant speciale afrper

gen week = honuge - 0838
recode kontakt (-1000/-1 = 0) (2/1000 = 1)
recode spec2 (96 = .)

gen Ncontacts = 1
gen Praccontacts = 1 if spec2 == 80

Collapse (sum) kontakt (sum) Ncontacts (sum) Praccontacts, by(pnr week)
sort pnr week

merge 1:1 pnr week using "E:\workdata\707063\Fattig Carina\data2008.dta"
drop if _merge == 1
drop _merge
recode kontakt Ncontacts  Praccontacts (.= 0)

gen ydelse = 1
rename kthj ydelsemodtaget
gen year = 2008

save "E:\workdata\707063\Fattig Carina\data2008.dta", replace

clear

*2009

use "E:\rawdata\707063\Grunddata\OFFL2017.dta"

gen yearfra = year(pti_vfra)
gen yeartil = year(pti_vtil)
gen monthfra = month(pti_vfra)
gen dayfra = day(pti_vfra)
gen doyfra = doy(pti_vfra)
gen doytil = doy(pti_vtil)

drop if yearfra > 2009
drop if yeartil < 2009

gen kthj = 1 if pti_tilstand_kode == 2518
replace kthj = 1 if pti_tilstand_kode == 5080
replace kthj = 1 if pti_tilstand_kode == 7050
replace kthj = 1 if pti_tilstand_kode == 7055
drop if kthj == .
destring pnr, replace

*Generering af ugeintervaller: For hvert interval angives, om vedkommende har været arbejdsløs i det pågældende interval.

gen datefra = doyfra - 332 + ((yearfra - 2009) * 365) 
gen datetil = doytil - 332 + ((yeartil - 2009) * 365)

gen weekfra = ceil(datefra/7)
gen weektil = ceil(datetil/7)

gen a0 = 1 if weekfra <= -9 & weektil >= -9 
gen a1 = 1 if weekfra <= -8 & weektil >= -8 
gen a2 = 1 if weekfra <= -7 & weektil >= -7 
gen a3 = 1 if weekfra <= -6 & weektil >= -6
gen a4 = 1 if weekfra <= -5 & weektil >= -5
gen a5 = 1 if weekfra <= -4 & weektil >= -4
gen a6 = 1 if weekfra <= -3 & weektil >= -3 
gen a7 = 1 if weekfra <= -2 & weektil >= -2 
gen a8 = 1 if weekfra <= -1 & weektil >= -1 
gen a9 = 1 if weekfra <= 0 & weektil >= 0
gen a10 = 1 if weekfra <= 1 & weektil >= 1 
gen a11 = 1 if weekfra <= 2 & weektil >= 2
gen a12 = 1 if weekfra <= 3 & weektil >= 3 
gen a13 = 1 if weekfra <= 4 & weektil >= 4 


foreach var of varlist a0-a13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist a0-a13 {
               bysort pnr: egen a`var' = max(`var')
        }

drop yearfra-a13

egen sumkthj = rowtotal(aa6-aa13)
collapse (mean) aa0-sumkthj , by(pnr)
drop if sumkthj != 8 
drop sumkthj

save "E:\workdata\707063\Fattig Carina\FCafgraens.dta", replace

clear

*___________________________________________________________________________________________________________

use "E:\rawdata\707063\Grunddata\LMDB2009.dta", clear

gen doyfra = doy(eksd)
gen sample = 1 if doyfra >= 262 & doyfra <= 360

drop if sample == .

gen date = doyfra - 332
gen week = ceil(date/7)


gen f0 = 1 if week == -9
gen f1 = 1 if week == -8
gen f2 = 1 if week == -7
gen f3 = 1 if week == -6
gen f4 = 1 if week == -5
gen f5 = 1 if week == -4
gen f6 = 1 if week == -3
gen f7 = 1 if week == -2
gen f8 = 1 if week == -1
gen f9 = 1 if week == 0 
gen f10 = 1 if week == 1
gen f11 = 1 if week == 2
gen f12 = 1 if week == 3
gen f13 = 1 if week == 4

foreach var of varlist f0-f13 {
               replace `var' = 0 if `var' == .
        }

	foreach var of varlist f0-f13 {
               bysort pnr: egen a`var' = total(`var')
        }

gen atccode = substr(atc,3,1)
destring atccode, replace

gen bf0 = 1 if week == -9 & atccode == 5
gen bf1 = 1 if week == -8 & atccode == 5
gen bf2 = 1 if week == -7 & atccode == 5
gen bf3 = 1 if week == -6 & atccode == 5
gen bf4 = 1 if week == -5 & atccode == 5
gen bf5 = 1 if week == -4 & atccode == 5
gen bf6 = 1 if week == -3 & atccode == 5
gen bf7 = 1 if week == -2 & atccode == 5
gen bf8 = 1 if week == -1 & atccode == 5
gen bf9 = 1 if week == 0  & atccode == 5
gen bf10 = 1 if week == 1 & atccode == 5
gen bf11 = 1 if week == 2 & atccode == 5
gen bf12 = 1 if week == 3 & atccode == 5
gen bf13 = 1 if week == 4 & atccode == 5


foreach var of varlist bf0-bf13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist bf0-bf13 {
               bysort pnr: egen a`var' = total(`var')
        }

gen cf0 = 1 if week == -9 & atccode == 6
gen cf1 = 1 if week == -8 & atccode == 6
gen cf2 = 1 if week == -7 & atccode == 6
gen cf3 = 1 if week == -6 & atccode == 6
gen cf4 = 1 if week == -5 & atccode == 6
gen cf5 = 1 if week == -4 & atccode == 6
gen cf6 = 1 if week == -3 & atccode == 6
gen cf7 = 1 if week == -2 & atccode == 6
gen cf8 = 1 if week == -1 & atccode == 6
gen cf9 = 1 if week == 0  & atccode == 6
gen cf10 = 1 if week == 1 & atccode == 6
gen cf11 = 1 if week == 2 & atccode == 6
gen cf12 = 1 if week == 3 & atccode == 6
gen cf13 = 1 if week == 4 & atccode == 6

foreach var of varlist cf0-cf13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist cf0-cf13 {
               bysort pnr: egen a`var' = total(`var')
			   }
	
collapse (mean) af0-acf13 , by(pnr)

drop atccode- bf13
drop cf0-cf13
destring pnr, replace
sort pnr

merge 1:1 pnr using "E:\workdata\707063\Fattig Carina\FCafgraens.dta"
drop if _merge == 1
drop _merge
		

	foreach var of varlist af0-acf13 {
               replace `var' = 0 if `var' == .
        }
	
save "E:\workdata\707063\Fattig Carina\data2009.dta", replace
	
*___________________________________________________________________________________________________________


reshape long af abf acf aa, i(pnr) j(week)

rename af NO5NO6
rename abf NO5
rename acf NO6
rename aa kthj

save "E:\workdata\707063\Fattig Carina\data2009.dta", replace

clear

*______________________________________________________________________________________________________________

use "E:\rawdata\707063\Grunddata\SSSY2009.dta"

destring spec2 pnr honuge, replace
sort pnr

drop if honuge < 0938

drop if spec2 >= 94 & spec2 <= 97
drop ydlant speciale afrper

gen week = honuge - 0938
recode kontakt (-1000/-1 = 0) (2/1000 = 1)
recode spec2 (96 = .)

gen Ncontacts = 1
gen Praccontacts = 1 if spec2 == 80

collapse (sum) kontakt (sum) Ncontacts (sum) Praccontacts, by(pnr week)
sort pnr week

merge 1:1 pnr week using "E:\workdata\707063\Fattig Carina\data2009.dta"
drop if _merge == 1
drop _merge
recode kontakt Ncontacts  Praccontacts (.= 0)

gen ydelse = 1
rename kthj ydelsemodtaget
gen year = 2009

save "E:\workdata\707063\Fattig Carina\data2009.dta", replace

clear

*_________________________________________________________________________

*2010

use "E:\rawdata\707063\Grunddata\OFFL2017.dta"

gen yearfra = year(pti_vfra)
gen yeartil = year(pti_vtil)
gen monthfra = month(pti_vfra)
gen dayfra = day(pti_vfra)
gen doyfra = doy(pti_vfra)
gen doytil = doy(pti_vtil)

drop if yearfra > 2010
drop if yeartil < 2010

gen kthj = 1 if pti_tilstand_kode == 2518
replace kthj = 1 if pti_tilstand_kode == 5080
replace kthj = 1 if pti_tilstand_kode == 7050
replace kthj = 1 if pti_tilstand_kode == 7055
drop if kthj == .
destring pnr, replace

gen datefra = doyfra - 332 + ((yearfra - 2010) * 365) 
gen datetil = doytil - 332 + ((yeartil - 2010) * 365)

gen weekfra = ceil(datefra/7)
gen weektil = ceil(datetil/7)

gen a0 = 1 if weekfra <= -9 & weektil >= -9 
gen a1 = 1 if weekfra <= -8 & weektil >= -8 
gen a2 = 1 if weekfra <= -7 & weektil >= -7 
gen a3 = 1 if weekfra <= -6 & weektil >= -6
gen a4 = 1 if weekfra <= -5 & weektil >= -5
gen a5 = 1 if weekfra <= -4 & weektil >= -4
gen a6 = 1 if weekfra <= -3 & weektil >= -3 
gen a7 = 1 if weekfra <= -2 & weektil >= -2 
gen a8 = 1 if weekfra <= -1 & weektil >= -1 
gen a9 = 1 if weekfra <= 0 & weektil >= 0
gen a10 = 1 if weekfra <= 1 & weektil >= 1 
gen a11 = 1 if weekfra <= 2 & weektil >= 2
gen a12 = 1 if weekfra <= 3 & weektil >= 3 
gen a13 = 1 if weekfra <= 4 & weektil >= 4 


foreach var of varlist a0-a13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist a0-a13 {
               bysort pnr: egen a`var' = max(`var')
        }

drop yearfra-a13

egen sumkthj = rowtotal(aa6-aa13)
collapse (mean) aa0-sumkthj , by(pnr)
drop if sumkthj != 8 
drop sumkthj

save "E:\workdata\707063\Fattig Carina\FCafgraens.dta", replace

clear

*___________________________________________________________________________________________________________

use "E:\rawdata\707063\Grunddata\LMDB2010.dta", clear

gen doyfra = doy(eksd)
gen sample = 1 if doyfra >= 262 & doyfra <= 360

drop if sample == .


gen date = doyfra - 332
gen week = ceil(date/7)

gen f0 = 1 if week == -9
gen f1 = 1 if week == -8
gen f2 = 1 if week == -7
gen f3 = 1 if week == -6
gen f4 = 1 if week == -5
gen f5 = 1 if week == -4
gen f6 = 1 if week == -3
gen f7 = 1 if week == -2
gen f8 = 1 if week == -1
gen f9 = 1 if week == 0 
gen f10 = 1 if week == 1
gen f11 = 1 if week == 2
gen f12 = 1 if week == 3
gen f13 = 1 if week == 4

foreach var of varlist f0-f13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist f0-f13 {
               bysort pnr: egen a`var' = total(`var')
        }

gen atccode = substr(atc,3,1)
destring atccode, replace

gen bf0 = 1 if week == -9 & atccode == 5
gen bf1 = 1 if week == -8 & atccode == 5
gen bf2 = 1 if week == -7 & atccode == 5
gen bf3 = 1 if week == -6 & atccode == 5
gen bf4 = 1 if week == -5 & atccode == 5
gen bf5 = 1 if week == -4 & atccode == 5
gen bf6 = 1 if week == -3 & atccode == 5
gen bf7 = 1 if week == -2 & atccode == 5
gen bf8 = 1 if week == -1 & atccode == 5
gen bf9 = 1 if week == 0  & atccode == 5
gen bf10 = 1 if week == 1 & atccode == 5
gen bf11 = 1 if week == 2 & atccode == 5
gen bf12 = 1 if week == 3 & atccode == 5
gen bf13 = 1 if week == 4 & atccode == 5


foreach var of varlist bf0-bf13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist bf0-bf13 {
               bysort pnr: egen a`var' = total(`var')
        }

gen cf0 = 1 if week == -9 & atccode == 6
gen cf1 = 1 if week == -8 & atccode == 6
gen cf2 = 1 if week == -7 & atccode == 6
gen cf3 = 1 if week == -6 & atccode == 6
gen cf4 = 1 if week == -5 & atccode == 6
gen cf5 = 1 if week == -4 & atccode == 6
gen cf6 = 1 if week == -3 & atccode == 6
gen cf7 = 1 if week == -2 & atccode == 6
gen cf8 = 1 if week == -1 & atccode == 6
gen cf9 = 1 if week == 0  & atccode == 6
gen cf10 = 1 if week == 1 & atccode == 6
gen cf11 = 1 if week == 2 & atccode == 6
gen cf12 = 1 if week == 3 & atccode == 6
gen cf13 = 1 if week == 4 & atccode == 6


foreach var of varlist cf0-cf13 {
               replace `var' = 0 if `var' == .
        }

foreach var of varlist cf0-cf13 {
               bysort pnr: egen a`var' = total(`var')
			   }
	
collapse (mean) af0-acf13 , by(pnr)

drop atccode- bf13
drop cf0-cf13
destring pnr, replace
sort pnr

merge 1:1 pnr using "E:\workdata\707063\Fattig Carina\FCafgraens.dta"
drop if _merge == 1
drop _merge
		

	foreach var of varlist af0-acf13 {
               replace `var' = 0 if `var' == .
        }
	
save "E:\workdata\707063\Fattig Carina\data2010.dta", replace

	
*___________________________________________________________________________________________________________


reshape long af abf acf aa, i(pnr) j(week)

rename af NO5NO6
rename abf NO5
rename acf NO6
rename aa kthj

save "E:\workdata\707063\Fattig Carina\data2010.dta", replace

clear

*______________________________________________________________________________________________________________

use "E:\rawdata\707063\Grunddata\SSSY2010.dta"

destring spec2 pnr honuge, replace
sort pnr

drop if honuge < 1037

drop if spec2 >= 94 & spec2 <= 97
drop ydlant speciale afrper

gen week = honuge - 1037
recode kontakt (-1000/-1 = 0) (2/1000 = 1)
recode spec2 (96 = .)

gen Ncontacts = 1
gen Praccontacts = 1 if spec2 == 80

collapse (sum) kontakt (sum) Ncontacts (sum) Praccontacts, by(pnr week)
sort pnr week

merge 1:1 pnr week using "E:\workdata\707063\Fattig Carina\data2010.dta"
drop if _merge == 1
drop _merge
recode kontakt Ncontacts  Praccontacts (.= 0)

gen ydelse = 1
rename kthj ydelsemodtaget
gen year = 2010

save "E:\workdata\707063\Fattig Carina\data2010.dta", replace

clear

*_________________________________________________________________________

use "E:\workdata\707063\Fattig Carina\data2011.dta", clear
append using "E:\workdata\707063\Fattig Carina\data2007.dta"
append using "E:\workdata\707063\Fattig Carina\data2008.dta"
append using "E:\workdata\707063\Fattig Carina\data2009.dta"
append using "E:\workdata\707063\Fattig Carina\data2010.dta"

save "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre", replace

clear

*_____________________________________________________________________________

use "E:\rawdata\707063\Grunddata\BEF2011.dta"

bysort familie_id: egen hjemmebarn = max(plads)

gen enligmedbarn2011 = 1 if hjemmebarn == 3 & plads != 3 & familie_type == 5

recode familie_type (1/4=0) (5=1), gen(enlig2011) 

gen birthyear = year(foed_dag)
gen birthmonth = month(foed_dag)
gen birthday = day(foed_dag)

gen alder2011 = 2011 - birthyear
replace alder2011 = 2010 - birthyear if birthmonth == 12
replace alder2011 = 2010 - birthyear if birthmonth == 11 & birthday > 28

recode koen (2=1) (1=0), gen(kvinde)

recode ie_type (1=0) (2=1) (3=0), gen (indvandrer)
recode ie_type (1=0) (2=0) (3=1), gen (efterkommer)

drop birthyear birthmonth birthday
drop aegte_id- hjemmebarn

destring pnr, replace

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre"

drop if _merge == 1
drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre", replace

clear

use "E:\rawdata\707063\Grunddata\BEF2010.dta"

bysort familie_id: egen hjemmebarn = max(plads)

gen enligmedbarn2010 = 1 if hjemmebarn == 3 & plads != 3 & familie_type == 5

recode familie_type (1/4=0) (5=1), gen(enlig2010) 

gen birthyear = year(foed_dag)
gen birthmonth = month(foed_dag)
gen birthday = day(foed_dag)

gen alder2010 = 2010 - birthyear
replace alder2010 = 2009 - birthyear if birthmonth == 12
replace alder2010 = 2009 - birthyear if birthmonth == 11 & birthday > 28

drop birthyear birthmonth birthday
drop aegte_id- hjemmebarn

destring pnr, replace

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre"

drop if _merge == 1
drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre", replace

clear

use "E:\rawdata\707063\Grunddata\BEF2009.dta"

bysort familie_id: egen hjemmebarn = max(plads)

gen enligmedbarn2009 = 1 if hjemmebarn == 3 & plads != 3 & familie_type == 5

recode familie_type (1/4=0) (5=1), gen(enlig2009) 

gen birthyear = year(foed_dag)
gen birthmonth = month(foed_dag)
gen birthday = day(foed_dag)

gen alder2009 = 2009 - birthyear
replace alder2009 = 2008 - birthyear if birthmonth == 12
replace alder2009 = 2008 - birthyear if birthmonth == 11 & birthday > 28

drop birthyear birthmonth birthday
drop aegte_id- hjemmebarn

destring pnr, replace

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre"

drop if _merge == 1
drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre", replace

clear

use "E:\rawdata\707063\Grunddata\BEF2008.dta"

bysort familie_id: egen hjemmebarn = max(plads)

gen enligmedbarn2008 = 1 if hjemmebarn == 3 & plads != 3 & familie_type == 5

recode familie_type (1/4=0) (5=1), gen(enlig2008) 

gen birthyear = year(foed_dag)
gen birthmonth = month(foed_dag)
gen birthday = day(foed_dag)

gen alder2008 = 2008 - birthyear
replace alder2008 = 2007 - birthyear if birthmonth == 12
replace alder2008 = 2007 - birthyear if birthmonth == 11 & birthday > 28

drop birthyear birthmonth birthday
drop aegte_id- hjemmebarn

destring pnr, replace

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre"

drop if _merge == 1
drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre", replace

clear

use "E:\rawdata\707063\Grunddata\BEF2007.dta"

bysort familie_id: egen hjemmebarn = max(plads)

gen enligmedbarn2007 = 1 if hjemmebarn == 3 & plads != 3 & familie_type == 5

recode familie_type (1/4=0) (5=1), gen(enlig2007) 

gen birthyear = year(foed_dag)
gen birthmonth = month(foed_dag)
gen birthday = day(foed_dag)

gen alder2007 = 2007 - birthyear
replace alder2007 = 2006 - birthyear if birthmonth == 12
replace alder2007 = 2006 - birthyear if birthmonth == 11 & birthday > 28

drop birthyear birthmonth birthday
drop aegte_id- hjemmebarn

destring pnr, replace

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre"

drop if _merge == 1
drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre", replace

gen alder = alder2011 if year == 2011
replace alder = alder2010 if year == 2010
replace alder = alder2009 if year == 2009
replace alder = alder2008 if year == 2008
replace alder = alder2007 if year == 2007

gen enligmedbarn = enligmedbarn2011 if year == 2011
replace enligmedbarn = enligmedbarn2010 if year == 2010
replace enligmedbarn = enligmedbarn2009 if year == 2009
replace enligmedbarn = enligmedbarn2008 if year == 2008
replace enligmedbarn = enligmedbarn2007 if year == 2007

gen enlig = enlig2011 if year == 2011
replace enlig = enlig2010 if year == 2010
replace enlig = enlig2009 if year == 2009
replace enlig = enlig2008 if year == 2008
replace enlig = enlig2007 if year == 2007

drop enligmedbarn2007- alder2010
replace enligmedbarn = 0 if enligmedbarn ==. & enlig != .
replace enligmedbarn2011 = 0 if enligmedbarn2011 ==. & enlig2011 != .

save "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre", replace

clear

use "E:\rawdata\707063\Grunddata\UDDF2017.dta"

gen fromyear = year(hf_vfra)
gen toyear = year(hf_vtil)

drop if fromyear > 2011
drop if toyear < 2011

recode hfaudd (1=0) (1006/1023=0) (1106/1123=0) (1208/1523=0) (200=0) (205=0) (210=0) (2508/2511=0)
replace hfaudd = 1 if hfaudd > 0

gen udd2011 = hfaudd 

destring pnr, replace

collapse (max) udd2011, by(pnr)

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre"

drop if _merge == 1
drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre", replace

clear

use "E:\rawdata\707063\Grunddata\UDDF2017.dta"

gen fromyear = year(hf_vfra)
gen toyear = year(hf_vtil)

drop if fromyear > 2010
drop if toyear < 2010

recode hfaudd (1=0) (1006/1023=0) (1106/1123=0) (1208/1523=0) (200=0) (205=0) (210=0) (2508/2511=0)
replace hfaudd = 1 if hfaudd > 0

gen udd2010 = hfaudd 

destring pnr, replace

collapse (max) udd2010, by(pnr)

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre"

drop if _merge == 1
drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre", replace

use "E:\rawdata\707063\Grunddata\UDDF2017.dta"

gen fromyear = year(hf_vfra)
gen toyear = year(hf_vtil)

drop if fromyear > 2009
drop if toyear < 2009

recode hfaudd (1=0) (1006/1023=0) (1106/1123=0) (1208/1523=0) (200=0) (205=0) (210=0) (2508/2511=0)
replace hfaudd = 1 if hfaudd > 0

gen udd2009 = hfaudd 

destring pnr, replace

collapse (max) udd2009, by(pnr)

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre"

drop if _merge == 1
drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre", replace

use "E:\rawdata\707063\Grunddata\UDDF2017.dta"

gen fromyear = year(hf_vfra)
gen toyear = year(hf_vtil)

drop if fromyear > 2008
drop if toyear < 2008

recode hfaudd (1=0) (1006/1023=0) (1106/1123=0) (1208/1523=0) (200=0) (205=0) (210=0) (2508/2511=0)
replace hfaudd = 1 if hfaudd > 0

gen udd2008 = hfaudd 

destring pnr, replace

collapse (max) udd2008, by(pnr)

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre"

drop if _merge == 1
drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre", replace

use "E:\rawdata\707063\Grunddata\UDDF2017.dta"

gen fromyear = year(hf_vfra)
gen toyear = year(hf_vtil)

drop if fromyear > 2007
drop if toyear < 2007

recode hfaudd (1=0) (1006/1023=0) (1106/1123=0) (1208/1523=0) (200=0) (205=0) (210=0) (2508/2511=0)
replace hfaudd = 1 if hfaudd > 0

gen udd2007 = hfaudd 

destring pnr, replace

collapse (max) udd2007, by(pnr)

merge 1:m pnr using "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre"

drop if _merge == 1
drop _merge

save "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre", replace

gen udd = udd2011 if year == 2011
replace udd = udd2010 if year == 2010
replace udd = udd2009 if year == 2009
replace udd = udd2008 if year == 2008
replace udd = udd2007 if year == 2007

drop udd2010 udd2009 udd2008 udd2007

save "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre", replace


clear

use "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre", clear

merge m:1 pnr using "E:\workdata\707063\Fattig Carina\FCafgraens.dta"

drop if _merge == 2
drop _merge

replace diagnosisnum = 0 if diagnosisnum == .

save "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre", replace


cd "E:\workdata\707063\Fattig Carina"


**************Analyser*****************************


clear

use "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre", clear

*Overview over distribution over years.

gen cons = 1
bysort pnr: egen totalcons = sum(cons)
tab totalcons
drop cons totalcons

clear 

*
use "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre", clear

cd "E:\workdata\707063\Fattig Carina"

egen pers = group (pnr year)
egen time = group (year week)
xtset pers week
recode week (0/9 = 0) (10/13 = 1), gen(beforeafterdummy_general)
recode week (0/5 = .) (6/9 = 0) (10/13 = 1), gen(beforeafterdummy)
replace NO5NO6 = 1 if NO5NO6 > 1
replace NO5 = 1 if NO5 > 1
replace NO6 = 1 if NO6 > 1
replace Ncontacts = 1 if Ncontacts > 1
replace Praccontacts = 1 if Praccontacts > 1
recode year (2007/2010 = 0) (2011 = 1), gen(treatmentyear)

gen enligkvindemedbarn = 1 if enligmedbarn == 1 & kvinde == 1
replace enligkvindemedbarn = 0 if enligmedbarn == 0
replace enligkvindemedbarn = 0 if enligmedbarn == 1 & kvinde == 0

gen enligioevrigt = 1 if enlig ==1 & enligmedbarn == 0
replace  enligioevrigt = 1 if enligmedbarn == 1 & kvinde == 0
replace  enligioevrigt = 0 if enligmedbarn == 1 & kvinde == 1
replace enligioevrigt = 0 if enlig == 0

xtset, clear

xtset pnr time


merge 1:1 week using "E:\workdata\707063\Fattig Carina\Parallel trends.dta"

drop _merge

save "E:\workdata\707063\Fattig Carina\Parallel trends.dta", replace

*Creating Figure S1.

bysort year: sum NO5NO6
bysort treatmentyear week: sum NO5NO6

bysort week: egen NO5NO6plac2 = mean(NO5NO6) if treatmentyear==0

collapse (mean) NO5NO6plac2 if treatmentyear == 0, by(week)
replace week = week - 9

twoway (scatter NO5NO6 week) (scatter NO5NO6plac1 week) (scatter NO5NO6plac2 week) (scatter NO5NO6plac3 week) (scatter NO5NO6plac4 week), scheme(s2mono)

rename NO5NO6 Treatment
rename NO5NO6plac1 Placebo1
rename NO5NO6plac2 Placebo2
rename NO5NO6plac3 Placebo3
rename NO5NO6plac4 Placebo4
label variable Treatment "Treatment"
label variable Placebo1 "Placebo1"
label variable Placebo2 "Placebo2"
label variable Placebo3 "Placebo3"
label variable Placebo4 "Placebo4"
twoway (scatter Treatment week) (scatter Placebo1 week) (scatter Placebo2 week) (scatter Placebo3 week) (scatter Placebo4 week), scheme(s2mono)
twoway (scatter Treatment week) (line Treatment week) (scatter Placebo1 week) (line Placebo1 week) (scatter Placebo2 week) (line Placebo2 week) (scatter Placebo3 week) (line Placebo3 week) (scatter Placebo4 week) (line Placebo4 week) if week > -4, xline(0.5) scheme(s2mono)

twoway (scatter Treatment week) (line Treatment week) (scatter Placebo1 week) (line Placebo1 week) (scatter Placebo2 week) (line Placebo2 week) (scatter Placebo3 week) (line Placebo3 week) (scatter Placebo4 week) (line Placebo4 week) if week > -4, xline(0.5) xlabel(#8) ytitle(Average recipience of antidepressants) scheme(s2mono)

clear

*Placebo analysis 2

use "E:\workdata\707063\Fattig Carina\data20072011_placebo_andre", clear

cd "E:\workdata\707063\Fattig Carina"

egen pers = group (pnr year)
egen time = group (year week)
xtset pers week
recode week (0/9 = 0) (10/13 = 1), gen(beforeafterdummy_general)
recode week (0/5 = .) (6/9 = 0) (10/13 = 1), gen(beforeafterdummy)
replace NO5NO6 = 1 if NO5NO6 > 1
replace NO5 = 1 if NO5 > 1
replace NO6 = 1 if NO6 > 1
replace Ncontacts = 1 if Ncontacts > 1
replace Praccontacts = 1 if Praccontacts > 1
recode year (2007/2010 = 0) (2011 = 1), gen(treatmentyear)

gen enligkvindemedbarn = 1 if enligmedbarn == 1 & kvinde == 1
replace enligkvindemedbarn = 0 if enligmedbarn == 0
replace enligkvindemedbarn = 0 if enligmedbarn == 1 & kvinde == 0

gen enligioevrigt = 1 if enlig ==1 & enligmedbarn == 0
replace  enligioevrigt = 1 if enligmedbarn == 1 & kvinde == 0
replace  enligioevrigt = 0 if enligmedbarn == 1 & kvinde == 1
replace enligioevrigt = 0 if enlig == 0

xtset, clear

xtset pnr time

*Table S5.
eststo: xtlogit NO5NO6 c.beforeafterdummy#c.treatmentyear c.treatmentyear i.week, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummy#c.treatmentyear c.treatmentyear i.week kvinde alder udd indvandrer efterkommer enligioevrigt, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummy#c.treatmentyear c.treatmentyear i.week kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn, vce(cluster pnr)

esttab using placeboothers.rtf, compress star(* 0.05 ** 0.01 *** 0.001) se ar2
eststo clear

*Table S8.
eststo: xtlogit NO5NO6 c.beforeafterdummy#c.treatmentyear c.treatmentyear, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummy#c.treatmentyear c.treatmentyear  kvinde alder udd indvandrer efterkommer enligioevrigt, vce(cluster pnr)
eststo: xtlogit NO5NO6 c.beforeafterdummy#c.treatmentyear c.treatmentyear kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn, vce(cluster pnr)

esttab using placeboothers.rtf, compress star(* 0.05 ** 0.01 *** 0.001) se ar2
eststo clear

*Estimates for Table 3
xtlogit NO5NO6 i.beforeafterdummy#c.treatmentyear c.treatmentyear i.week kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn, vce(cluster pnr)

margins beforeafterdummy

*Estimates for Table S12 - heterogeneous treatment effects
eststo: xtlogit NO5NO6 i.beforeafterdummy#c.treatmentyear c.treatmentyear i.week kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn if diagnosisnum == 0, vce(cluster pnr) 

predictnl pnew = predict(pr)
sum pnew if beforeafterdummy == 0 & treatmentyear == 0
sum pnew if beforeafterdummy == 1 & treatmentyear == 0
sum pnew if beforeafterdummy == 0 & treatmentyear == 1
sum pnew if beforeafterdummy == 1 & treatmentyear == 1

eststo: xtlogit NO5NO6 i.beforeafterdummy#c.treatmentyear c.treatmentyear i.week kvinde alder udd indvandrer efterkommer enligioevrigt enligkvindemedbarn if diagnosisnum == 1, vce(cluster pnr) 

predictnl pnew1 = predict(pr)
sum pnew1 if beforeafterdummy == 0 & treatmentyear == 0
sum pnew1 if beforeafterdummy == 1 & treatmentyear == 0
sum pnew1 if beforeafterdummy == 0 & treatmentyear == 1
sum pnew1 if beforeafterdummy == 1 & treatmentyear == 1

***************************************************************************************************************************************************************
